Servlet 使用介绍(1)
说明
本篇介绍java web中比较重要的一个技术:servlet。servlet是一种对用户请求动态响应的一个技术,是java web的核心一环。对于一般服务性质的纯后台服务应用而言,或许整个应用是由若干个servlet组成
,而其他方面表现较弱。
servlet
一般的我们的应用都会发布到一个服务应用中,即一个servlet容器中,常见的servlet容器有:Tomcat,JBoss,WebLogic等。我们通过servlet容器处理用户请求,当用户向我们的某个servlet发起请求时,servlet容器会调用并转发请求结果到该servlet,同时会将servlet返回结果回馈给请求方(用户)。其实,servlet本质就是一个接口,我们可以通过注册servlet服务,实现servlet接口,用于监听各种状态先的信息交互。如下,仅仅介绍tomcat中的servlet的相关使用(主要是其他也没用过)。
Tomcat中为servlet的使用提供了一个调用的jar包:sevlet-api.jar ,该jar包位于Tomcat根目录的lib目录下(所有和Tomcat相关的Jar包均放在这里),如下为servlet-api.jar中的包信息:
servlet-api.jar
--javax.servlet //核心
--javax.servlet.annotation
--javax.servlet.descriptor
--javax.servlet.http //基于http的实现
可以看到,这里主要石油javax.servlet和javax.servlet.http两个包组成实现。其中javax.servlet.Servlet接口为api的核心,servlet容器通过调用其定义方法,实现网络中客户端和服务端之前通信的中转传递,Servlet提供了五个方法,其中三个方法是由servlet容器调用。如下,servlet的源码:
package javax.servlet;
import java.io.IOException;
public interface Servlet {
/**
*由sevler容器调用,servlet对象初始化后,servlet容器调用此方法
*
*/
public void init(ServletConfig config) throws ServletException;
/**
*提供的一个获取ServletConfig对象的方法,该方法由我们自己实现,返回的对象即是init
*的方法传入的参数。
*
*/
public ServletConfig getServletConfig();
/**
*该方法需要在init方法成功调用后才可正常调用(即客户端请求的时候必须姚该servlet以及
*初始化成功)
*
*/
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException;
/**
*返回当前servlet的创建信息包括创建者等(为一个String字串)
*
*/
public String getServletInfo();
/**
*由servlet容器调用,当一个servlet容器被销毁时调用,可以用于清理销毁servlet中的占
*用的一些资源。
*
*/
public void destroy();
}
Servlet接口为最基础的Servlet组件,可供我们最大限度的对servlet进行定制.servlet-api.jar中也为我们提供了它的两个替代实现:一个是javax.servlet.GenericServlet是其实现类,完成它的最基础的一些逻辑实现,但保留其对于网络通信的处理,交由我们自行处理;另一个是javax.servlet.http.HttpServlet,该类是GenericServlet的子类,是对GenericServlet的一种扩展,其对于Servlet的servive方法进行了处理,主要用于Http请求。如下,给出这两个类的实现源码:
- GenericServlet
该抽象类并未对servive方法作处理。
public abstract class GenericServlet implements Servlet, ServletConfig,
java.io.Serializable{
private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
private static ResourceBundle lStrings =
ResourceBundle.getBundle(LSTRING_FILE);
private transient ServletConfig config;
public GenericServlet() { }
/**
*销毁方法
*/
public void destroy() {
}
/**
*获取servlet初始化传入参数
*
*/
public String getInitParameter(String name) {
ServletConfig sc = getServletConfig();
if (sc == null) {
throw new IllegalStateException(
lStrings.getString("err.servlet_config_not_initialized"));
}
return sc.getInitParameter(name);
}
/**
*servlet参数参数名获取
*/
public Enumeration<String> getInitParameterNames() {
ServletConfig sc = getServletConfig();
if (sc == null) {
throw new IllegalStateException(
lStrings.getString("err.servlet_config_not_initialized"));
}
return sc.getInitParameterNames();
}
/**
*实现Servlet方法返回初始化获取的ServletConfig对象
*/
public ServletConfig getServletConfig() {
return config;
}
/**
*获取ServletContext对象(Servlet上下文),是在ServletConfig中获取
*/
public ServletContext getServletContext() {
ServletConfig sc = getServletConfig();
if (sc == null) {
throw new IllegalStateException(
lStrings.getString("err.servlet_config_not_initialized"));
}
return sc.getServletContext();
}
public String getServletInfo() {
return "";
}
/**
*实现Servlet初始化方法,并回调init()无参方法,可供实现者监听状态.
*
*
*/
public void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();
}
/**
* 子类可通过重写监听Servlet对象初始化状态.
*/
public void init() throws ServletException {
}
public void log(String msg) {
getServletContext().log(getServletName() + ": "+ msg);
}
public void log(String message, Throwable t) {
getServletContext().log(getServletName() + ": " + message, t);
}
public abstract void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException;
public String getServletName() {
ServletConfig sc = getServletConfig();
if (sc == null) {
throw new IllegalStateException(
lStrings.getString("err.servlet_config_not_initialized"));
}
return sc.getServletName();
}
}
- HttpServlet
是GenericServlet的子类,对service添加了Http的处理,一般适用于Http通信的Servlet,是较为常用的一种Servlet.
package javax.servlet.http;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public abstract class HttpServlet extends GenericServlet{
private static final String METHOD_DELETE = "DELETE";
private static final String METHOD_HEAD = "HEAD";
private static final String METHOD_GET = "GET";
private static final String METHOD_OPTIONS = "OPTIONS";
private static final String METHOD_POST = "POST";
private static final String METHOD_PUT = "PUT";
private static final String METHOD_TRACE = "TRACE";
private static final String HEADER_IFMODSINCE = "If-Modified-Since";
private static final String HEADER_LASTMOD = "Last-Modified";
private static final String LSTRING_FILE =
"javax.servlet.http.LocalStrings";
private static ResourceBundle lStrings =
ResourceBundle.getBundle(LSTRING_FILE);
public HttpServlet() { }
/**
*Http协议Get方式请求回调
*
*/
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_get_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
protected long getLastModified(HttpServletRequest req) {
return -1;
}
/**
*Http协议HHEAD(只请求页面的头部)方式请求回调
*/
protected void doHead(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
NoBodyResponse response = new NoBodyResponse(resp);
doGet(req, response);
response.setContentLength();
}
/**
*Http协议POST方式请求回调
*/
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_post_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
/**
*Http协议Put方式请求回调
*/
protected void doPut(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_put_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
/**
*Http协议Delete方式请求回调
*/
protected void doDelete(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_delete_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
private Method[] getAllDeclaredMethods(Class<?> c) {
if (c.equals(javax.servlet.http.HttpServlet.class)) {
return null;
}
Method[] parentMethods = getAllDeclaredMethods(c.getSuperclass());
Method[] thisMethods = c.getDeclaredMethods();
if ((parentMethods != null) && (parentMethods.length > 0)) {
Method[] allMethods =
new Method[parentMethods.length + thisMethods.length];
System.arraycopy(parentMethods, 0, allMethods, 0,
parentMethods.length);
System.arraycopy(thisMethods, 0, allMethods, parentMethods.length,
thisMethods.length);
thisMethods = allMethods;
}
return thisMethods;
}
protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
Method[] methods = getAllDeclaredMethods(this.getClass());
boolean ALLOW_GET = false;
boolean ALLOW_HEAD = false;
boolean ALLOW_POST = false;
boolean ALLOW_PUT = false;
boolean ALLOW_DELETE = false;
boolean ALLOW_TRACE = true;
boolean ALLOW_OPTIONS = true;
for (int i=0; i<methods.length; i++) {
Method m = methods[i];
if (m.getName().equals("doGet")) {
ALLOW_GET = true;
ALLOW_HEAD = true;
}
if (m.getName().equals("doPost"))
ALLOW_POST = true;
if (m.getName().equals("doPut"))
ALLOW_PUT = true;
if (m.getName().equals("doDelete"))
ALLOW_DELETE = true;
}
String allow = null;
if (ALLOW_GET)
allow=METHOD_GET;
if (ALLOW_HEAD)
if (allow==null) allow=METHOD_HEAD;
else allow += ", " + METHOD_HEAD;
if (ALLOW_POST)
if (allow==null) allow=METHOD_POST;
else allow += ", " + METHOD_POST;
if (ALLOW_PUT)
if (allow==null) allow=METHOD_PUT;
else allow += ", " + METHOD_PUT;
if (ALLOW_DELETE)
if (allow==null) allow=METHOD_DELETE;
else allow += ", " + METHOD_DELETE;
if (ALLOW_TRACE)
if (allow==null) allow=METHOD_TRACE;
else allow += ", " + METHOD_TRACE;
if (ALLOW_OPTIONS)
if (allow==null) allow=METHOD_OPTIONS;
else allow += ", " + METHOD_OPTIONS;
resp.setHeader("Allow", allow);
}
/**
*处理TRACE请求
*/
protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
int responseLength;
String CRLF = "\r\n";
StringBuilder buffer = new StringBuilder("TRACE ").append(req.getRequestURI())
.append(" ").append(req.getProtocol());
Enumeration<String> reqHeaderEnum = req.getHeaderNames();
while( reqHeaderEnum.hasMoreElements() ) {
String headerName = reqHeaderEnum.nextElement();
buffer.append(CRLF).append(headerName).append(": ")
.append(req.getHeader(headerName));
}
buffer.append(CRLF);
responseLength = buffer.length();
resp.setContentType("message/http");
resp.setContentLength(responseLength);
ServletOutputStream out = resp.getOutputStream();
out.print(buffer.toString());
}
/**
*http协议处理
*/
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();
if (method.equals(METHOD_GET)) {
long lastModified = getLastModified(req);
if (lastModified == -1) {
// servlet doesn't support if-modified-since, no reason
// to go through further expensive logic
doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
if (ifModifiedSince < lastModified) {
// If the servlet mod time is later, call doGet()
// Round down to the nearest second for a proper compare
// A ifModifiedSince of -1 will always be less
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
}
} else if (method.equals(METHOD_HEAD)) {
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
} else if (method.equals(METHOD_POST)) {
doPost(req, resp);
} else if (method.equals(METHOD_PUT)) {
doPut(req, resp);
} else if (method.equals(METHOD_DELETE)) {
doDelete(req, resp);
} else if (method.equals(METHOD_OPTIONS)) {
doOptions(req,resp);
} else if (method.equals(METHOD_TRACE)) {
doTrace(req,resp);
} else {
//
// Note that this means NO servlet supports whatever
// method was requested, anywhere on this server.
//
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
}
}
private void maybeSetLastModified(HttpServletResponse resp,
long lastModified) {
if (resp.containsHeader(HEADER_LASTMOD))
return;
if (lastModified >= 0)
resp.setDateHeader(HEADER_LASTMOD, lastModified);
}
/**
*重写父类方法,转化为http协议处理,调用自有http处理
*/
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
HttpServletRequest request;
HttpServletResponse response;
if (!(req instanceof HttpServletRequest &&
res instanceof HttpServletResponse)) {
throw new ServletException("non-HTTP request or response");
}
request = (HttpServletRequest) req;
response = (HttpServletResponse) res;
service(request, response);
}
}
// file private
class NoBodyResponse extends HttpServletResponseWrapper {
private static final ResourceBundle lStrings
= ResourceBundle.getBundle("javax.servlet.http.LocalStrings");
private NoBodyOutputStream noBody;
private PrintWriter writer;
private boolean didSetContentLength;
private boolean usingOutputStream;
// file private
NoBodyResponse(HttpServletResponse r) {
super(r);
noBody = new NoBodyOutputStream();
}
// file private
void setContentLength() {
if (!didSetContentLength) {
if (writer != null) {
writer.flush();
}
setContentLength(noBody.getContentLength());
}
}
public void setContentLength(int len) {
super.setContentLength(len);
didSetContentLength = true;
}
public ServletOutputStream getOutputStream() throws IOException {
if (writer != null) {
throw new IllegalStateException(
lStrings.getString("err.ise.getOutputStream"));
}
usingOutputStream = true;
return noBody;
}
public PrintWriter getWriter() throws UnsupportedEncodingException {
if (usingOutputStream) {
throw new IllegalStateException(
lStrings.getString("err.ise.getWriter"));
}
if (writer == null) {
OutputStreamWriter w = new OutputStreamWriter(
noBody, getCharacterEncoding());
writer = new PrintWriter(w);
}
return writer;
}
}
/*
* Servlet output stream that gobbles up all its data.
*/
// file private
class NoBodyOutputStream extends ServletOutputStream {
private static final String LSTRING_FILE =
"javax.servlet.http.LocalStrings";
private static ResourceBundle lStrings =
ResourceBundle.getBundle(LSTRING_FILE);
private int contentLength = 0;
// file private
NoBodyOutputStream() {}
// file private
int getContentLength() {
return contentLength;
}
public void write(int b) {
contentLength++;
}
public void write(byte buf[], int offset, int len)
throws IOException
{
if (len >= 0) {
contentLength += len;
} else {
// This should have thrown an IllegalArgumentException, but
// changing this would break backwards compatibility
throw new IOException(lStrings.getString("err.io.negativelength"));
}
}
}
如上,Servlet对于用户的请求获取和返回主要是通过方法:
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException;
实现的。这里,servlet容器在调用时传入两个参数:一个ServletRequest对象,用于接收用户请求信息;一个ServletResponse对象,用于返回请求用户信息。而HttpServlet对于对象则更改为HttpServletRequest和HttpServletResponse对象。其实这两个接口是同样在对应的ServletRequest和ServletResponse接口的基础上扩展的。
Servlet 使用介绍(1)的更多相关文章
- servlet简单介绍
什么是Servlet? servlet是一种Java编程语言类,用于扩展托管通过请求 - 响应编程模型访问的应用程序的服务器的功能.尽管servlet可以响应任何类型的请求,但它们通常用于扩展Web服 ...
- Servlet过滤器介绍之原理分析
zhangjunhd 的BLOG 写留言去学院学习发消息 加友情链接进家园 加好友 博客统计信息 51CTO博客之星 用户名:zhangjunhd 文章数:110 评论数:858 访问量:19 ...
- Introducation of Servlet filter(servlet过滤器介绍 )
本文章向大家介绍Servlet Filter,主要包括 Servlet Filter使用实例.应用技巧.基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下. 过滤器是一个可以转换 ...
- Servlet 使用介绍(2)
说明 本篇由于介绍Servlet容器回传请求方法service(ServletRequest req, ServletResponse res);传入参数用户请求参数request和请求返回参数res ...
- 10.Servlet简单介绍
1.什么是Servlet * Servlet是javaweb的三大组件之一,它属于动态资源.Servlet的作用是处理请求,服务器会把接收到的请求交给Servlet来处理,在Servlet种通常需要: ...
- J2EE的13个规范之(三) Servlet简单介绍
Servlet是一种server端脚本,它是一个特殊的Java类,继承自HttpServlet.开发中主要用于处理和响应client的请求. Servlet在容器中执行,事实上例的和销毁创建由容器进行 ...
- Servlet基本介绍和使用
基本概念 Servlet又称为Java Servlet是一个基于java技术的web组件,运行在服务器端,用于生成动态的内容.Servlet是平台独立的java类,编写一个Servlet实际上就是按照 ...
- Servlet 简单介绍
来源于菜鸟教程http://www.runoob.com/servlet/servlet-intro.html Servlet 简介 Servlet 是什么? Servlet(Server Apple ...
- 一、动态网络编程的概念 二、Tomcat服务器搭建 三、Servlet组件介绍
一.动态网络编程的概念 动态网页:结合了HTML以外的高级程序编程语言和数据库技术生成的页面. 动态网页编程技术: ASP,PHP,JSP HTTP协议:规范浏览器和服务器之间通信的数据格式. 浏览器 ...
随机推荐
- ubuntu下安装tomcat,shutdown时报错:./catalina.sh:1:eval:/home/xxx/jdk/jre/bin/java:not found
该问题可能导致tomcat启动成功了,但是浏览器输入http://127.0.0.1:8080无法显示tomcat的欢迎界面 打开Tomcat安装目录下的bin文件下的setclasspath.sh, ...
- Nginx+Tomcat8+Memcached实现负载均衡及session共享
1> 基础环境 简易拓扑图: 2> 部署Tomcat [root@node01 ~]# ll -h ~ |egrep 'jdk|tomcat'-rw-r--r-- 1 root root ...
- Servlet是什么?JSP和Servlet的区别。Servlet的生命周期。
Servlet(Server Applet),全称Java Servlet, 是用Java编写的服务器端程序.而这些Sevlet都要实现Servlet这个借口.其主要功能在于交互式地浏览和修改数据,生 ...
- docker chromium/chrome: error while loading shared libraries: libXcursor.so.2: cannot open shared object file: No such file or directory
在运行 pyppeteer 的时候,因为依赖浏览器,出现上述情况. 解决办法: 一般我们在Linux下执行某些外部程序的时候可能会提示找不到共享库的错误.原因一般有两个, 一个是操作系统里确实没有包含 ...
- 版本控制工具——Git常用操作
本文引自:https://juejin.im/post/5c206f1bf265da615114c15a
- Server基本语句的用法
1.创建数据库 create database databaseName use databaseName go /* 转到指定数据库 */ 2.创建表 create table tableNa ...
- 48-创建 overlay 网络
上一节我们搭建好实验环境,配置并运行了consul,今天开始创建 overlay 网络. 在 host1 中创建 overlay 网络 ov_net1: -d overlay 指定 driver 为 ...
- C++ std::list 和 std::forward_list 的差别及其成员函数差异对比
主要差别: list 是双向链表,forward_list 是双向链表. 成员函数差异: 函数名 list forward_list back() has no size() has no inser ...
- 如何用node.js中的ejs写入页面_以6.19京东秒杀的商品为例
用erpress搭建好基本框架后,在自己新建的express文件夹下将会生成;bin,public,routes,views,app.js,package.json,node_modules目录哟!, ...
- windowsServer---- 在iis 上安装网站
1.找到信息服务IIS 管理器如图: 2.进入后进行配置 3.添加本地网站 配置网站 如果域名没有解析的话,可以在添加一个 端口用于测试 点击浏览就行查看 如果报错 解决:找到目录浏览,并启动 点击 ...