Servlet initializes and processes HTTP requests. servlet initializes requests.

Source: Internet
Author: User

Servlet initializes and processes HTTP requests. servlet initializes requests.

The previous article details several core Servlet-related interfaces and classes. When we write Servlet classes by ourselves, we generally need to inherit the HttpServlet class to implement init (), doGet (), doPost (). When will the Servlet class be initialized (init () when we write it ())? When do I execute doGet (), doPost (), and other methods? This article focuses on these two issues.

1. Servlet Initialization

 A previous article titled web. as mentioned in the xml file configuration explanation, the Servlet class we write must be configured in the <servlet-class> label, you also need to configure several labels, such as <servlet-name>, <load-on-startup>, and <servlet-mapping>. Describes the servlet configuration in the web. xml file. The MainServlet class in the <servlet-class> label is the class we write.

When we use Tomcat to start the entire web project, when the <load-on-startup> label is configured and the number in it is> = 0, the MainServlet class will be loaded, create an instance of the MainServlet class and call the init () method for initialization. In the init () method, you can use the getServletConfig () method to obtain the ServletConfig object, and then use the getInitParameter () method of this object to obtain the configuration information in the <init-param> label, the configuration file is parsed. This Initialization is performed only once throughout the servlet lifecycle. If the <load-on-startup> label or number <0 is not configured, tomcat will not create MainServlet class instances at startup, and of course it will not call the init () method for initialization. When the user first accesses it, the class will be loaded and initialized, therefore, loading may be slow during the first access. During development, the <load-on-startup> label is usually configured. If there are multiple <servlet>, configure different values for <load-on-startup> ......), Tomcat loads Servlet classes in ascending order.

Because the Tomcat container uses the "single-instance multi-thread" method to process multiple requests, whether it is an instance of the MainServlet class created at Tomcat startup or an instance of the MainServlet class created at the first access, during the entire life cycle of the MainServlet class, its instances are created only once, and only the init () method Initialization is executed once. After that, when multiple users access this Servlet, they share this instance. each user has its own thread, so it is "Single Instance multithreading ". Of course, this may cause thread security issues. In the future, I will introduce the Servlet thread security issues separately.

2. process HTTP requests

After we start the web project with Tomcat, we can enter the local address in the browser, to open the page (if the <weblcome-file-list> label is configured, the page display in the label is preferentially loaded, as shown in.

You can see that the address entered in the browser is "http: // localhost: 8080/yqzl/product_input.action", where "http" is the protocol name and "localhost" is the local address, "8080" is the server. the port number configured in xml. "yqzl" is the name of the web project, and "product_input.action" is the page pointing. Then, how does the browser find our MainServlet Class Based on the address and process the request? The following describes the configurations in the web. xml file.

First, locate the <url-pattern> (ing address), that is, the row that won the bid (1), based on the "product_input.action" at the end of the address; then, based on the <url-pattern>, you can find the <servlet-name> (servlet name), that is, the row that won the bid (2). the servlet name (yqzl) correspond to the above name, and then find the row marked in figure (3). Finally, find the path of the <servlet-class> label below, that is, the row marked in figure (4, the MainServlet class we wrote.

When processing a request, the Tomcat container will call the HttpServletRequest req, HttpServletResponse resp method of the HttpServlet class. This method will determine the type of request sent by the user, is a "POST" request, "GET" request, or other requests, and then executes the doPost () method, doGet () method, or other method of the MainServlet Class Based on the Request type. The Tomcat container encapsulates the data requested by the user into the HttpServletRequest object. After the server processes the user request, it returns the result to the HttpServletResponse object, the two objects are passed as parameters to the doPost (), doGet (), or other methods, and the result information is returned to the page. The above is the general process for processing requests.

Reprinted please indicate the source http://www.cnblogs.com/Y-oung/p/8428536.html

Work, study, exchange or have any questions, please contact: yy1340128046@163.com: yy1340128046

 

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.