zoukankan      html  css  js  c++  java
  • Httpservlet源码说明

    上一篇看了Servlet接口,现在来看下我们经常涉及的Httpservlet:

    /**
     *
     * Provides an abstract class to be subclassed to create
     * an HTTP servlet suitable for a Web site. A subclass of
     * <code>HttpServlet</code> must override at least 
     * one method, usually one of these:
     *
     * <ul>
     * <li> <code>doGet</code>, if the servlet supports HTTP GET requests
     * <li> <code>doPost</code>, for HTTP POST requests
     * <li> <code>doPut</code>, for HTTP PUT requests
     * <li> <code>doDelete</code>, for HTTP DELETE requests
     * <li> <code>init</code> and <code>destroy</code>, 
     * to manage resources that are held for the life of the servlet
     * <li> <code>getServletInfo</code>, which the servlet uses to
     * provide information about itself 
     * </ul>
     *
     * <p>There's almost no reason to override the <code>service</code>
     * method. <code>service</code> handles standard HTTP
     * requests by dispatching them to the handler methods
     * for each HTTP request type (the <code>do</code><i>XXX</i>
     * methods listed above).
     *
     * <p>Likewise, there's almost no reason to override the 
     * <code>doOptions</code> and <code>doTrace</code> methods.
     * 
     * <p>Servlets typically run on multithreaded servers,
     * so be aware that a servlet must handle concurrent
     * requests and be careful to synchronize access to shared resources.
     * Shared resources include in-memory data such as
     * instance or class variables and external objects
     * such as files, database connections, and network 
     * connections.
     * See the
     * <a href="http://java.sun.com/Series/Tutorial/java/threads/multithreaded.html">
     * Java Tutorial on Multithreaded Programming</a> for more
     * information on handling multiple threads in a Java program.
     *
     * @author    Various
     */
    
    
    
    public abstract class HttpServlet extends GenericServlet
        implements java.io.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(LSTRING_FILE);
       
       

    HttpServlet是一个抽象类,它是为了实现http协议的servlet,所有继承此抽象类的servlet必须实现以下方法中的一种:

    doGet;

    doPost;

    doPut;

    doDelete;

    init 和 destroy;

    没有必要去重写service方法,service处理标准的http请求,根据不同的HTTP请求类型分发给以上的doXXX方法进行处理;

    其他的描述和上一篇的servlet一样,在此就不多费篇幅咯。

    下面重点看看doGet方法和doPost方法:

    1、doGet:

        /**
         *
         * Called by the server (via the <code>service</code> method) to
         * allow a servlet to handle a GET request. 
         *
         * <p>Overriding this method to support a GET request also
         * automatically supports an HTTP HEAD request. A HEAD
         * request is a GET request that returns no body in the
         * response, only the request header fields.
         *
         * <p>When overriding this method, read the request data,
         * write the response headers, get the response's writer or 
         * output stream object, and finally, write the response data.
         * It's best to include content type and encoding. When using
         * a <code>PrintWriter</code> object to return the response,
         * set the content type before accessing the
         * <code>PrintWriter</code> object.
         *
         * <p>The servlet container must write the headers before
         * committing the response, because in HTTP the headers must be sent
         * before the response body.
         *
         * <p>Where possible, set the Content-Length header (with the
         * {@link javax.servlet.ServletResponse#setContentLength} method),
         * to allow the servlet container to use a persistent connection 
         * to return its response to the client, improving performance.
         * The content length is automatically set if the entire response fits
         * inside the response buffer.
         *
         * <p>When using HTTP 1.1 chunked encoding (which means that the response
         * has a Transfer-Encoding header), do not set the Content-Length header.
         *
         * <p>The GET method should be safe, that is, without
         * any side effects for which users are held responsible.
         * For example, most form queries have no side effects.
         * If a client request is intended to change stored data,
         * the request should use some other HTTP method.
         *
         * <p>The GET method should also be idempotent, meaning
         * that it can be safely repeated. Sometimes making a
         * method safe also makes it idempotent. For example, 
         * repeating queries is both safe and idempotent, but
         * buying a product online or modifying data is neither
         * safe nor idempotent. 
         *
         * <p>If the request is incorrectly formatted, <code>doGet</code>
         * returns an HTTP "Bad Request" message.
         * 
         *
         * @param req    an {@link HttpServletRequest} object that
         *            contains the request the client has made
         *            of the servlet
         *
         * @param resp    an {@link HttpServletResponse} object that
         *            contains the response the servlet sends
         *            to the client
         * 
         * @exception IOException    if an input or output error is 
         *                detected when the servlet handles
         *                the GET request
         *
         * @exception ServletException    if the request for the GET
         *                    could not be handled
         *
         * 
         * @see javax.servlet.ServletResponse#setContentType
         *
         */
    
        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);
        }
        }

    ^此方法由服务器调用允许一个servlet去处理一个GET请求;

    ^重写此方法去支持一个GET请求,同时也支持一个HEAD请求,一个HEAD请求是一个得到响应中没有返回任何主体的GET请求,只有请求头字段;

    ^当载入此方法时,读取请求数据,写入响应头,获取response的writer对象或者输出流对象,最终写入response数据,最好包含内容类型和编码方式;

    ^servlet容器必须在提交response之前写头文件,因为在HTTP协议中文件头必须在response body体之前提交;

    ^set方法是安全的,也就是说这个方法没有任何需要用户负责任的副作用;比如:大多数的查询都没有副作用,当你试图改变数据库的数据时,则需要使用其他方法。也就是说GET方法只用于查询某些简单的数据;

    2、doPost方法:

      /**
         *
         * Called by the server (via the <code>service</code> method)
         * to allow a servlet to handle a POST request.
         *
         * The HTTP POST method allows the client to send
         * data of unlimited length to the Web server a single time
         * and is useful when posting information such as
         * credit card numbers.
         *
         * <p>When overriding this method, read the request data,
         * write the response headers, get the response's writer or output
         * stream object, and finally, write the response data. It's best 
         * to include content type and encoding. When using a
         * <code>PrintWriter</code> object to return the response, set the 
         * content type before accessing the <code>PrintWriter</code> object. 
         *
         * <p>The servlet container must write the headers before committing the
         * response, because in HTTP the headers must be sent before the 
         * response body.
         *
         * <p>Where possible, set the Content-Length header (with the
         * {@link javax.servlet.ServletResponse#setContentLength} method),
         * to allow the servlet container to use a persistent connection 
         * to return its response to the client, improving performance.
         * The content length is automatically set if the entire response fits
         * inside the response buffer.  
         *
         * <p>When using HTTP 1.1 chunked encoding (which means that the response
         * has a Transfer-Encoding header), do not set the Content-Length header. 
         *
         * <p>This method does not need to be either safe or idempotent.
         * Operations requested through POST can have side effects for
         * which the user can be held accountable, for example, 
         * updating stored data or buying items online.
         *
         * <p>If the HTTP POST request is incorrectly formatted,
         * <code>doPost</code> returns an HTTP "Bad Request" message.
         *
         *
         * @param req    an {@link HttpServletRequest} object that
         *            contains the request the client has made
         *            of the servlet
         *
         * @param resp    an {@link HttpServletResponse} object that
         *            contains the response the servlet sends
         *            to the client
         * 
         * @exception IOException    if an input or output error is 
         *                detected when the servlet handles
         *                the request
         *
         * @exception ServletException    if the request for the POST
         *                    could not be handled
         *
         *
         * @see javax.servlet.ServletOutputStream
         * @see javax.servlet.ServletResponse#setContentType
         *
         *
         */
    
        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(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
        } else {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
        }
        }

    此时你会发现与上面的doGet方法的描述非常类似。唯一的不同是doPost会处理修改数据的请求。

    其他的四种方法我就不一一列举了,大同小异,大家自己看一下。

  • 相关阅读:
    HTML5拖动画布/拖放
    随着通信和编程,它是一门艺术系列6(技术的情况)
    POJ 3652 &amp; ZOJ 2934 &amp; HDU 2721 Persistent Bits(数学 元)
    使用注解实现 bean 转 csv
    hdu4956 Poor Hanamichi
    教你如何赚你的第一桶金
    【Java 它 JVM】对象的创建过程
    c#控制台应用程序-“进入指定日期检查出星期几”
    迎接和书,这是一个很好的满足
    bloom filter与dawgdic(一种trie树)
  • 原文地址:https://www.cnblogs.com/bossen/p/5944243.html
Copyright © 2011-2022 走看看