Servlet以及HttpServlet的源码分析

说明:在编写 Servlet 时需要用到两个用于所有 Servlet 的基本软件包:javax.servlet 和 javax.servlet.http。下面主要介绍 javax.servlet 提供的 Servlet 以及 javax.servlet.http 提供的 HTTP Servlet 应用编程接口。
Servlet源码:
package javax.servlet;
import java.io.IOException;
// Referenced classes of package javax.servlet:
// ServletException, ServletConfig, ServletRequest, ServletResponse
public interface Servlet
{
    public abstract void init(ServletConfig servletconfig)
        throws ServletException;
    public abstract ServletConfig getServletConfig();
    public abstract void service(ServletRequest servletrequest, ServletResponse servletresponse)
        throws ServletException, IOException;
    public abstract String getServletInfo();
    public abstract void destroy();
}
以上便是Servlet的源码,Servlet本身是一个接口,首先介绍一下Java Servlet的基本方法。
init() 方法
    在Servlet的生命周期中,仅执行一次 init() 方法,即在服务器装入Servlet时执行。通过配置服务器,可以设定在启动服务器或客户机首次访问Servlet 时装入Servlet。无论有多少客户机访问Servlet,都不会重复执行 init()。
service() 方法 
    service()方法是Servlet的主体部分。客户对一个HttpServlet对象的每次请求都会调用该对象的service() 方法,并传递给这个方法一个"请求"对象和一个"响应"对象作为参数。"请求"对象提供有关请求的信息,而"响应"对象提供了一个将响应信息返回给浏览器的通信途径。javax.servlet 软件包中的相关类为ServletRequest和ServletResponse,而javax.servlet.http 软件包中的相关类为HttpServletRequest 和 HttpServletResponse。Servlet 通过这些对象与服务器通信并最终与客户机通信。Servlet 能通过调用"请求"对象的方法获知客户机环境、服务器环境的信息和所有由客户机提供的信息;通过调用"响应"对象的方法,Servlet可以向客户机发送响应。
    HttpServlet中的service()方法默认的服务功能是调用与 HTTP 请求的方法相应的 do 功能。例如,如果 HTTP 请求方法为 GET,则默认情况下就调用 doGet()。当一个客户通过HTML表单发出一个HTTP POST请求时,doPost()方法被调用。与POST请求相关的参数作为一个单独的HTTP 请求从浏览器发送到服务器。当需要修改服务器端的数据时,应该使用doPost()方法。
    Servlet的响应可以是下列几种类型: 
    一个输出流,浏览器根据它的内容类型(如text/HTML)进行解释。 
    一个HTTP错误响应,重定向到另一个URL、Servlet和JSP。
destroy() 方法 
    destroy() 方法仅执行一次,即在服务器停止且卸载Servlet 时执行该方法。当服务器卸载 Servlet 时,将在所有 service() 方法调用完成后,或在指定的时间间隔过后调用 destroy() 方法。一个Servlet 在运行service() 方法时可能会产生其他的线程,因此在调用 destroy() 方法时,必须确认这些线程已终止或完成。
GetServletConfig()方法 
    GetServletConfig()方法返回一个 ServletConfig 对象,该对象用来返回初始化参数和ServletContext。ServletContext 接口提供有关Servlet 的环境信息。
GetServletInfo()方法 
    GetServletInfo()方法是一个可选的方法,它提供有关Servlet 的信息,如作者、版本、版权。

下面是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.*; // Referenced classes of package javax.servlet.http: //            NoBodyResponse, HttpServletRequest, HttpServletResponse public abstract class HttpServlet extends GenericServlet     implements Serializable {     public HttpServlet()     {     }     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(javax/servlet/http/HttpServlet))             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 = (new StringBuilder()).append(allow).append(", HEAD").toString();         if(ALLOW_POST)             if(allow == null)                 allow = "POST";             else                 allow = (new StringBuilder()).append(allow).append(", POST").toString();         if(ALLOW_PUT)             if(allow == null)                 allow = "PUT";             else                 allow = (new StringBuilder()).append(allow).append(", PUT").toString();         if(ALLOW_DELETE)             if(allow == null)                 allow = "DELETE";             else                 allow = (new StringBuilder()).append(allow).append(", DELETE").toString();         if(ALLOW_TRACE)             if(allow == null)                 allow = "TRACE";             else                 allow = (new StringBuilder()).append(allow).append(", TRACE").toString();         if(ALLOW_OPTIONS)             if(allow == null)                 allow = "OPTIONS";             else                 allow = (new StringBuilder()).append(allow).append(", OPTIONS").toString();         resp.setHeader("Allow", allow);     }     protected void doTrace(HttpServletRequest req, HttpServletResponse resp)         throws ServletException, IOException     {         String CRLF = "\r\n";         String responseString = (new StringBuilder()).append("TRACE ").append(req.getRequestURI()).append(" ").append(req.getProtocol()).toString();         for(Enumeration reqHeaderEnum = req.getHeaderNames(); reqHeaderEnum.hasMoreElements();)         {             String headerName = (String)reqHeaderEnum.nextElement();             responseString = (new StringBuilder()).append(responseString).append(CRLF).append(headerName).append(": ").append(req.getHeader(headerName)).toString();         }         responseString = (new StringBuilder()).append(responseString).append(CRLF).toString();         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);     }     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"); } 通过HttpServlet的源码我们看到,HttpServlet与Servelt没有关联关系,而是继承了GenericServlet,其实GenericServlet是一个抽象类,该类已经实现了Servlet, ServletConfig, Serializable这三个接口,GenericServlet的源码我就不在这里展示了,大家有兴趣可以再去研究一下GenericServlet的源码便知。 通过HttpServlet的源码我们发现,该类有两个service()方法,下面我们一一说明: 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); } 通过代码我们发现,这里把相应的request和response转换为了基于HTTP协议的相应对象。 Servlet接口中定义了一个service方法,HttpServlet对该方法进行了实现,将ServletRequest和ServletResponse转换为HttpServletRequest和HttpServletResponse。 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 / 1000 * 1000)) {                     // 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);         } } 通过该service()方法可以发现客户端的请求先提交给Service方法时,先获取提交的方法名,根据方法名调用相应的具体方法。如果重写了该方法,那么就不会根据方法名调用其他具体的方法了。 下面看一下HttpServlet类内部的几个DoXXX()方法: protected void doXXX(HttpServletRequest req, HttpServletResponse resp)         throws ServletException, IOException     {         String protocol = req.getProtocol();         String msg = lStrings.getString("http.method_xxx_not_supported");         if (protocol.endsWith("1.1")) {             resp.sendError(405, msg);         } else {             resp.sendError(400, msg);         } } 发现doXXX()方法只是判断协议类型,然后抛出相应的异常,其他什么都没做。所以,在实现自己的Servlet时,需要重写这些方法,以符合自己的需求。一般不需要重写service方法。 这里就对Servlet和HttpServlet就分析这些,如果大家还有什么疑问,可以再行讨论,谢谢!

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值