HttpServlet根据客户发出的HTTP请求,生成响应的HTTP响应结果。HttpServlet首先必须读取HTTP请求的内容。Servlet容器负责创建HttpRequest对象,并把HTTP请求信息封装到HttpRequest对象中,这大大简化了HttpServlet解析请求数据的工作量。如果没有HttpServletRequest,HttpServlet只能直接处理Web客户发出的原始的字符串数据。
HttpServlet继承GenericServlet。GenericServlet为抽象类,HttpServlet也是一个抽象类,但是HttpServlet中定义了关于HTTP协议的支持方法。其中定义了支持的请求方法,与请求处理的具体逻辑。
支持的请求方法有:
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";
protected void doGet(HttpServletRequest req, HttpServletResponse resp) protected void doHead(HttpServletRequest req, HttpServletResponse resp) protected void doPost(HttpServletRequest req, HttpServletResponse resp) protected void doPut(HttpServletRequest req, HttpServletResponse resp) protected void doDelete(HttpServletRequest req,HttpServletResponse resp) protected void doOptions(HttpServletRequest req,HttpServletResponse resp) protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
具体处理逻辑(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); } }
先获取请求的http协议,如果是http1.1则返回405,否则返回400.这里只是简单的处理,所有自定义的servlet一般都继承HttpServlet,重写请求方法的处理逻辑。doGet的参数有HttpServletRequest和HttpServletResponse。HttpServletRequest包含了HTTP的请求信息,HttpServletResponse则包含的是响应信息。(HttpServletRequest,HttpServletResponse都是由web容器创建的。)
处理处理请求的各个协议外,还有一个请求接收的总方法service
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); }
调用
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); } }
可见请求是以service按照请求的方法分发到各个请求方法处理逻辑中去的。
long lastModified = getLastModified(req);
这里调用了本类的protected方法:getLastModified
protected long getLastModified(HttpServletRequest req) { return -1; }
此方法返回都是-1。此方法和缓存有关。具体逻辑如下:
在http协议中,浏览器对访问过的页面缓存后,它将会在以后访问该页面时,将会根据LastModified头字段指定的时间值生成If-Modified-Since头字段,作为缓存页面的最新更新时间。如果网页的最后修改时间比If-Modified-Since头字段指定的时间早的话,web服务器就会请求的页面,如果自If-modified-Since指定的时间以来,网页内容没有被修改的话,服务器就会返回一个304响应头,一次告诉浏览器继续使用已缓存的页面。 继承HttpServlet的servlet程序在接收到客户端的GET请求后,HttpServlet的重载service方法会先调用getLastModified方法,根据这个方法的返回值来决定是否要调用doGet方法和生成Last-Modified头字段。主要有以下三种决定方式:
1.如果getLastModified方法的返回值是一个负数的话,不管客户端的请求信息如何,service方法都会调用doGet方法生成响应信息返回给客户端。
2.如果getLastModified方法的返回值是一个正数,并且客户端的请求消息中没有包含If-Modified-Since头字段的(这种情况是第一次访问该页面时)或者是请求消息中包含If-Modified-Since头字段,但是返回值比If-Modified-Since头字段指定的时间新的话,则service方法调用doGet方法生成响应信息和Last-Modified消息头返回给客户端。