HttpServlet源码

来源:互联网 发布:丹尼斯·约翰逊数据 编辑:程序博客网 时间:2024/04/28 02:13

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;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.NoBodyResponse;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");    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);        this.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;        } else {            Method[] parentMethods = this.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 = this.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 allow = 0; allow < methods.length; ++allow) {            Method m = methods[allow];            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 var13 = null;        if(ALLOW_GET && var13 == null) {            var13 = "GET";        }        if(ALLOW_HEAD) {            if(var13 == null) {                var13 = "HEAD";            } else {                var13 = var13 + ", HEAD";            }        }        if(ALLOW_POST) {            if(var13 == null) {                var13 = "POST";            } else {                var13 = var13 + ", POST";            }        }        if(ALLOW_PUT) {            if(var13 == null) {                var13 = "PUT";            } else {                var13 = var13 + ", PUT";            }        }        if(ALLOW_DELETE) {            if(var13 == null) {                var13 = "DELETE";            } else {                var13 = var13 + ", DELETE";            }        }        if(ALLOW_TRACE) {            if(var13 == null) {                var13 = "TRACE";            } else {                var13 = var13 + ", TRACE";            }        }        if(ALLOW_OPTIONS) {            if(var13 == null) {                var13 = "OPTIONS";            } else {                var13 = var13 + ", OPTIONS";            }        }        resp.setHeader("Allow", var13);    }    protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        String CRLF = "\r\n";        String responseString = "TRACE " + req.getRequestURI() + " " + req.getProtocol();        String out;        for(Enumeration reqHeaderEnum = req.getHeaderNames(); reqHeaderEnum.hasMoreElements(); responseString = responseString + CRLF + out + ": " + req.getHeader(out)) {            out = (String)reqHeaderEnum.nextElement();        }        responseString = responseString + CRLF;        int responseLength = responseString.length();        resp.setContentType("message/http");        resp.setContentLength(responseLength);        ServletOutputStream out1 = resp.getOutputStream();        out1.print(responseString);        out1.close();    }    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        String method = req.getMethod();        long errMsg;        if(method.equals("GET")) {            errMsg = this.getLastModified(req);            if(errMsg == -1L) {                this.doGet(req, resp);            } else {                long ifModifiedSince = req.getDateHeader("If-Modified-Since");                if(ifModifiedSince < errMsg / 1000L * 1000L) {                    this.maybeSetLastModified(resp, errMsg);                    this.doGet(req, resp);                } else {                    resp.setStatus(304);                }            }        } else if(method.equals("HEAD")) {            errMsg = this.getLastModified(req);            this.maybeSetLastModified(resp, errMsg);            this.doHead(req, resp);        } else if(method.equals("POST")) {            this.doPost(req, resp);        } else if(method.equals("PUT")) {            this.doPut(req, resp);        } else if(method.equals("DELETE")) {            this.doDelete(req, resp);        } else if(method.equals("OPTIONS")) {            this.doOptions(req, resp);        } else if(method.equals("TRACE")) {            this.doTrace(req, resp);        } else {            String errMsg1 = lStrings.getString("http.method_not_implemented");            Object[] errArgs = new Object[]{method};            errMsg1 = MessageFormat.format(errMsg1, errArgs);            resp.sendError(501, errMsg1);        }    }    private void maybeSetLastModified(HttpServletResponse resp, long lastModified) {        if(!resp.containsHeader("Last-Modified")) {            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 var6) {            throw new ServletException("non-HTTP request or response");        }        this.service(request, response);    }}


0 0