一:定义:Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层。
二:
三.servlet的任务:读取客户端(浏览器)发送的显式的数据。这包括网页上的 HTML 表单,或者也可以是来自 applet 或自定义的 HTTP 客户端程序的表单。
● 读取客户端(浏览器)发送的隐式的 HTTP 请求数据。这包括 cookies、媒体类型和浏览器能理解的压缩格式等等。
● 处理数据并生成结果。这个过程可能需要访问数据库,执行 RMI 或 CORBA 调用,调用 Web 服务,或者直接计算得出对应的响应。
● 发送显式的数据(即文档)到客户端(浏览器)。该文档的格式可以是多种多样的,包括文本文件(HTML 或 XML)、二进制文件(GIF 图像)、Excel 等。
● 发送隐式的 HTTP 响应到客户端(浏览器)。这包括告诉浏览器或其他客户端被返回的文档类型(例如 HTML),设置 cookies 和缓存参数,以及其他类似的任务。
四.每个servlet首先需要继承HttpServlet类
HttpServlet定义如下:
package javax.servlet.http;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.Enumeration;
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
implements Serializable
{
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(“javax.servlet.http.LocalStrings”);
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(405, msg);
else
resp.sendError(400, msg);
}
protected long getLastModified(HttpServletRequest req)
{
return -1L;
}
protected void doHead(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
NoBodyResponse response = new NoBodyResponse(resp);
doGet(req, response);
response.setContentLength();
}
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(405, msg);
else
resp.sendError(400, msg);
}
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(405, msg);
else
resp.sendError(400, msg);
}
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(405, msg);
else
resp.sendError(400, msg);
}
private Method[] getAllDeclaredMethods(Class c)
{
if (c.equals(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(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 == null)) allow = "GET";
if (ALLOW_HEAD)
if (allow == null) allow = "HEAD"; else
allow = allow + ", HEAD";
if (ALLOW_POST)
if (allow == null) allow = "POST"; else
allow = allow + ", POST";
if (ALLOW_PUT)
if (allow == null) allow = "PUT"; else
allow = allow + ", PUT";
if (ALLOW_DELETE)
if (allow == null) allow = "DELETE"; else
allow = allow + ", DELETE";
if (ALLOW_TRACE)
if (allow == null) allow = "TRACE"; else
allow = allow + ", TRACE";
if (ALLOW_OPTIONS) {
if (allow == null) allow = "OPTIONS"; else
allow = allow + ", OPTIONS";
}
resp.setHeader("Allow", allow);
}
protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String CRLF = “\r\n”;
String responseString = “TRACE ” + req.getRequestURI() + ” ” + req.getProtocol();
Enumeration reqHeaderEnum = req.getHeaderNames();
while (reqHeaderEnum.hasMoreElements()) {
String headerName = (String)reqHeaderEnum.nextElement();
responseString = responseString + CRLF + headerName + ": " + req.getHeader(headerName);
}
responseString = responseString + CRLF;
int responseLength = responseString.length();
resp.setContentType("message/http");
resp.setContentLength(responseLength);
ServletOutputStream out = resp.getOutputStream();
out.print(responseString);
out.close();
}
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();
if (method.equals("GET")) {
long lastModified = getLastModified(req);
if (lastModified == -1L)
{
doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader("If-Modified-Since");
if (ifModifiedSince < lastModified / 1000L * 1000L)
{
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(304);
}
}
}
else if (method.equals("HEAD")) {
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
}
else if (method.equals("POST")) {
doPost(req, resp);
}
else if (method.equals("PUT")) {
doPut(req, resp);
}
else if (method.equals("DELETE")) {
doDelete(req, resp);
}
else if (method.equals("OPTIONS")) {
doOptions(req, resp);
}
else if (method.equals("TRACE")) {
doTrace(req, resp);
}
else
{
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
}
private void maybeSetLastModified(HttpServletResponse resp, long lastModified)
{
if (resp.containsHeader(“Last-Modified”))
return;
if (lastModified >= 0L)
resp.setDateHeader(“Last-Modified”, lastModified);
}
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
HttpServletRequest request;
HttpServletResponse response;
try
{
request = (HttpServletRequest)req;
response = (HttpServletResponse)res;
} catch (ClassCastException e) {
throw new ServletException(“non-HTTP request or response”);
}
service(request, response);
}
}
有几个重要的方法如下
1.servlet每次收到客户端发送过来的请求,先创建用于封装HTTP请求消息的HttpServletRequest对象和HTTP响应消息的HtttpServletResponse对象,然后调用service方法将请求和响应对象作为参赛传递进去。
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
//获取请求方式,然后执行对象请求方式的方法
String method = req.getMethod();
if (method.equals("GET")) {
long lastModified = getLastModified(req);
if (lastModified == -1L)
{
doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader("If-Modified-Since");
if (ifModifiedSince < lastModified / 1000L * 1000L)
{
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(304);
}
}
}
else if (method.equals("HEAD")) {
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
}
else if (method.equals("POST")) {
doPost(req, resp);
}
else if (method.equals("PUT")) {
doPut(req, resp);
}
else if (method.equals("DELETE")) {
doDelete(req, resp);
}
else if (method.equals("OPTIONS")) {
doOptions(req, resp);
}
else if (method.equals("TRACE")) {
doTrace(req, resp);
}
else
{
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
}
HttpServlet继承于父类GenericServlet
package javax.servlet;
import java.io.IOException;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.ResourceBundle;
public abstract class GenericServlet
implements Servlet, ServletConfig, Serializable
{
private static final String LSTRING_FILE = “javax.servlet.LocalStrings”;
private static ResourceBundle lStrings = ResourceBundle.getBundle(“javax.servlet.LocalStrings”);
private transient ServletConfig config;
public void destroy()
{
}
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);
}
public Enumeration getInitParameterNames()
{
ServletConfig sc = getServletConfig();
if (sc == null) {
throw new IllegalStateException(lStrings.getString(“err.servlet_config_not_initialized”));
}
return sc.getInitParameterNames();
}
public ServletConfig getServletConfig()
{
return this.config;
}
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 “”;
}
public void init(ServletConfig config)
throws ServletException
{
this.config = config;
init();
}
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 paramServletRequest, ServletResponse paramServletResponse)
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();
}
}
GenericServlet实现了Servlet、ServletConfig接口
这个类中比较重要的方法是init
public void init(ServletConfig config)
throws ServletException
{
this.config = config;
init();
}
public void init()
throws ServletException
{
}
我们经常写init方法的时候都会重写init()方法,第一个init方法是将ServletConfig参数传进来,而平时我们自己写的时候容易忘记写参数,就会导致把父类方法覆盖,所以这里有定义了一个无参数的init方法,平时重写的时候是覆盖了这个方法,自行的初始化代码就可以写在里面.
servlet的生命周期:
Servlet 生命周期可被定义为从创建直到毁灭的整个过程。以下是 Servlet 遵循的过程:
● Servlet 通过调用 init () 方法进行初始化。
● Servlet 调用 service() 方法来处理客户端的请求。
● Servlet 通过调用 destroy() 方法终止(结束)。
● 最后,Servlet 是由 JVM 的垃圾回收器进行垃圾回收的。