Java Web Development Fundamentals (1)-servlet

Source: Internet
Author: User

OverviewServlet Technology is an extension of the webserver, to understand this, you may first understand what is the webserver and its functions, especially its shortcomings are very helpful, can refer to: Click the Open link. Other than that. The servlet is also a special Java class written in the Java language servlet API. It executes in the servlet container. Its function is to parse the request, process the request, construct the response message, it is already on the Web service line This is also very easy to understand. There's so much in the concept.


servlet Container The container is responsible for managing the Servlet class, which contains the following: matching servlet, running the servlet life cycle, and so on. The relationship between a container and a servlet is like a restaurant and a chef. Chef can be divided into why Hunan, Fujian, Sichuan, Chinese pastry and so on. This is equivalent to a servlet class where customers order food through the waiter, and then the restaurant then finds the appropriate chef (Servlet) to cook according to the order. The chef then cooks the dish and passes it on to the customer (Response). The container is probably the equivalent of what the restaurant does, and the chef is positioned. Management chef. Here's a look at the structure of the Tomcat container (photo author Xilingpo).
Life cycle

The life cycle of a servlet needs to be created, initialized, run, and destroyed in four stages. These four phases are managed by a servlet container. The entire running process, for example, is seen.

This is because the servlet is a single-instance multithreading, so check that the instance exists for the request first. The following is a detailed analysis of each phase of the life cycle.


Create: When there are two cases when a servlet instance is created, by default the servlet instance is created when the first request arrives. Or it is set by the configuration file to instantiate when the container starts. Just need to be on the web. Add the following code:<loadon-startup>1</loadon-startup> in the XML file for example <Servlet></Servlet>. The values here represent the order in which the servlet is instantiated.


Initialize : Once the servlet instance is created, webserver will voluntarily invoke the init (servletconfig config) method to initialize the servlet. The configuration information for the servlet is included in the method parameter config. For example, an initialization parameter, which is created by the server.

<span style= "FONT-SIZE:18PX;" >        <span style= "font-family:fangsong_gb2312;" ><servlet>       <servlet-name>ClientIdValidateServlet</servlet-name>       <servlet-class >com.bjpowernode.drp.util.servlet.ClientIdValidateServlet</servlet-class></servlet>< servlet-mapping>        <servlet-name>ClientIdValidateServlet</servlet-name>        <url-pattern >/servlet/ClientIdValidateServlet</url-pattern></servlet-mapping></span></span>< /SPAN>
    run (Service) Webserver: The service (ServletRequest request,servletresponse Response) method of the instance is called during the services phase. The request object and the response object have server created and passed to the servlet instance. The request object encapsulates the information that the client sends to the server side. The response object encapsulates information that is sent to the client by the server.


    destroy: webserver must be guaranteed before the destroy () method is called. Let all the threads in the service () method that are executing in the instance exit or wait for these threads for a period of time.

Once the Destroy () method has been executed. Webserver will reject all newly arrived requests for the servlet instance, and the Destroy () method exits, and the servlet instance can be garbage collected.


    Summary : Servlets are at the heart of the Java Web and are important for understanding how it works and life cycle.

Despite watching the video. Checked the information. It took nearly a day, but the feeling was not particularly clear.

An issue about how to navigate to a servlet class file about its relationships with containers and containers and webserver, application server.

is even more chaotic!

Java Web Development Fundamentals (1)-servlet

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.