⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpservlet.java

📁 梅花雪树的经典制作
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     *     * @exception IOException   if an input or output error occurs     *                              while the servlet is handling the     *                              TRACE request     *     * @exception ServletException  if the request for the     *                                  TRACE cannot be handled     */    protected void doTrace(HttpServletRequest req, HttpServletResponse resp)         throws ServletException, IOException    {                int responseLength;                String CRLF = "\r\n";        String responseString = "TRACE "+ req.getRequestURI()+            " " + req.getProtocol();                Enumeration reqHeaderEnum = req.getHeaderNames();                while( reqHeaderEnum.hasMoreElements() ) {            String headerName = (String)reqHeaderEnum.nextElement();            responseString += CRLF + headerName + ": " +                req.getHeader(headerName);         }                responseString += CRLF;                responseLength = responseString.length();                resp.setContentType("message/http");        resp.setContentLength(responseLength);        ServletOutputStream out = resp.getOutputStream();        out.print(responseString);                out.close();        return;    }                    /**     * Receives standard HTTP requests from the public     * <code>service</code> method and dispatches     * them to the <code>do</code><i>XXX</i> methods defined in      * this class. This method is an HTTP-specific version of the      * {@link javax.servlet.Servlet#service} method. There's no     * need to override this method.     *     * @param req   the {@link HttpServletRequest} object that     *                  contains the request the client made of     *                  the servlet     *     * @param resp  the {@link HttpServletResponse} object that     *                  contains the response the servlet returns     *                  to the client                                     *     * @exception IOException   if an input or output error occurs     *                              while the servlet is handling the     *                              HTTP request     *     * @exception ServletException  if the HTTP request     *                                  cannot be handled     *      * @see javax.servlet.Servlet#service     */    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);        }    }    /*     * Sets the Last-Modified entity header field, if it has not     * already been set and if the value is meaningful.  Called before     * doGet, to ensure that headers are set before response data is     * written.  A subclass might have set this header already, so we     * check.     */    private void maybeSetLastModified(HttpServletResponse resp,                                      long lastModified) {        if (resp.containsHeader(HEADER_LASTMOD))            return;        if (lastModified >= 0)            resp.setDateHeader(HEADER_LASTMOD, lastModified);    }           /**     * Dispatches client requests to the protected     * <code>service</code> method. There's no need to     * override this method.     *      * @param req   the {@link HttpServletRequest} object that     *                  contains the request the client made of     *                  the servlet     *     * @param res   the {@link HttpServletResponse} object that     *                  contains the response the servlet returns     *                  to the client                                     *     * @exception IOException   if an input or output error occurs     *                              while the servlet is handling the     *                              HTTP request     *     * @exception ServletException  if the HTTP request cannot     *                                  be handled     *      * @see javax.servlet.Servlet#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);    }}/* * A response wrapper for use in (dumb) "HEAD" support. * This just swallows that body, counting the bytes in order to set * the content length appropriately.  All other methods delegate to the * wrapped HTTP Servlet Response object. */// file privateclass NoBodyResponse extends HttpServletResponseWrapper {    private NoBodyOutputStream                noBody;    private PrintWriter                        writer;    private boolean                        didSetContentLength;    // file private    NoBodyResponse(HttpServletResponse r) {        super(r);        noBody = new NoBodyOutputStream();    }    // file private    void setContentLength() {        if (!didSetContentLength)          super.setContentLength(noBody.getContentLength());    }    // SERVLET RESPONSE interface methods    public void setContentLength(int len) {        super.setContentLength(len);        didSetContentLength = true;    }    public ServletOutputStream getOutputStream() throws IOException {        return noBody;    }    public PrintWriter getWriter() throws UnsupportedEncodingException {        if (writer == null) {            OutputStreamWriter w;            w = new OutputStreamWriter(noBody, getCharacterEncoding());            writer = new PrintWriter(w);        }        return writer;    }}/* * Servlet output stream that gobbles up all its data. */ // file privateclass 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 {            // XXX            // isn't this really an IllegalArgumentException?                        String msg = lStrings.getString("err.io.negativelength");            throw new IOException(msg);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -