Tomcat implements the request method in the HTTP protocol

Source: Internet
Author: User

Tomcat is a Web server that implements the HTTP protocol.

HttpServlet is the core class of the entire Java EE system. All requests from the client have this class for processing and forwarding. It is mounted and managed by Tomcat based on the servlet configuration tag in Web. Xml.

The following is the servlet inheritance structure.

javax.servlet.http
Class HttpServlet
Java.lang.Object Javax.servlet.GenericServlet Javax.servlet.http.HttpServlet
    • All implemented Interfaces:

    • Serializable, Servlet, ServletConfig

The servlet life cycle starts with init (automatically called when Tomcat is started), and when the client sends an HTTP request, the service () processing method is called, and the Service method contains many HTTP request methods, such as Post,get,delete, Option,put,delete,trace,head. The implementation of these methods in the HTTP message request method column. When you finally close Tomcat, call the Destroy () method. (Servlet is a singleton)

650) this.width=650; "Title=" Click to view the source page "style=" width:594px;height:232px; "class=" currentimg "src=" http:/ Exiuims.tsci.com.cn/newsimg/2015-01-27/wkiol1thnfahgakdaafu--59s3m173.jpg "height=" 311 "width=" 428 "alt=" Wkiol1thnfahgakdaafu--59s3m173.jpg "/>

Here's a look at the implementation of three methods in Tomcat source code:

Initializing configuration information in Web. xml

public void init (ServletConfig config) throws servletexception {this.config = config;    This.init (); }

      //service () primarily calls the Doxxx method, xxx for the request method in the HTTP protocol.

   protected void service (httpservletrequest req, httpservletresponse  RESP)         throws servletexception, ioexception {         //Remove the method from the Req request and determine the method type          string method = req.getmethod ();         if   (Method.equals (method_get))  {                 //Get server Resource Last modified time lastmodified (one of the caching mechanisms)              long lastmodified = getlastmodified (req);             //-1 words do not support if-modified-since             if  (lastmodified == -1)  {          &Nbsp;      // servlet doesn ' T support if-modified-since,  no reason                 // to go through further expensive logic                 doget (REQ,&NBSP;RESP);             } else {             //return LastModified In response header, request header add a              //ifmodifiedsince, ask if the current page is up to date.                 long  ifmodifiedsince;                 try {          &nbSp;         ifmodifiedsince = req.getdateheader (HEADER_ ifmodsince);                 }  catch  (illegalargumentexception iae)  {                     // invalid date header  - proceed as if none                      //was set                     ifmodifiedsince  = -1;                 }                // If it is less than the specified time, it may be the latest                 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,&NBSP;RESP);                 } else {                     resp.setstatus (httpservletresponse.sc_not_modified);                 }             }        } else  if  (Method.equals (method_head))  {             long lastmodified = getlastmodified (req);             maybesetlastmodified (resp, lastmodified);           &Nbsp; dohead (REQ,&NBSP;RESP);        } else if  ( Method.equals (method_post))  {             DoPost (REQ,&NBSP;RESP);                     } else if  (Method.equals (method_put))  {             doput (REQ,&NBSP;RESP);                              } else if  (Method.equals (method_delete))  {             dodelete (REQ,&NBSP;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);   The general logic of       }    }    //is to go first to the data in the cache, Then call the appropriate method.

At the end of a request life cycle, rather than destroying the container, destroying the container is the Stopserver () method in the Catalina class. There is no specific way to find destroy. That's how it was destroyed? Does it correspond to a GC? A question? For comment.

public void Destroy () {}

Here are some of the methods and properties in HttpServlet:

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);

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) ......


This is the tomcat implementation that corresponds to the request method in the HTTP protocol. In other words: how many types of requests are there in Tomcat? In fact, the qualitative re-ask HTPP protocol in the understanding of the method. Each method has its own characteristics. Why does the HTTP protocol need this method? For example, which methods are safe and which methods are idempotent methods? (See--http Protocol RFC2616 Section 9.1 for specific differences). Perhaps know the rationale behind these, before you can know the essence of the HTTP protocol. I think an apple blew up Newton, and an agreement led the human race towards the internet age. comparable to Computer E=MC^2!!!

The HttpRFC2616 agreement is attached.



This article is from my blog blog, so be sure to keep this source http://thinklili.blog.51cto.com/10867130/1758256

Tomcat implements the request method in the HTTP protocol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.