Servlet details (2)

Source: Internet
Author: User

 

1. servlet Creation Time

Servlet isCannot run separatelyIt is called the servlet engine or web server.

Generally, the server creates only one servlet instance object for the length of the Servlet request on the client. That is to say, once the servlet instance object is created, it will reside in the memory, for other subsequent request services, the servlet instance object will not be destroyed until the Web Container exits.

 

Once a servlet object is created, the init method is executed. Once destroyed, the destroy method is executed. You can use these two methods to observe the lifecycle of the servlet object.

Therefore, if a servlet demo is compiled, the servlet object will not be created when the server is started. Only http ://... /demo, The init method is executed and the object is created. After we turn off the browser, this object will not disappear and will be accessed later. It is still the original servlet object.

This object will be destroyed only when we stop the server.

However, each access request calls the servlet service method, and each request, the Servlet Request creates an httpservletrequest request object and an httpservletresponse object, and then takes these two objects as parameters to the service method, the service method then calls the doxxx Method Based on the Request Method.

The lifecycle of httpservletrequest request objects and httpservletresponse objects is very short. For example, if Sina's website has hundreds of millions of requests every day, hundreds of millions of objects will be generated, but the objects will be destroyed immediately after the request ends, as long as there are not so many requests at the same time, the pressure on the server memory is not very high.

 

2. The servlet object can also be created when the web server is started, instead of the first access.

  <servlet>    <servlet-name>ServletDemo</servlet-name>    <servlet-class>cn.school.ServletDemo</servlet-class>    <load-on-startup>1</load-on-startup>  </servlet>


The structs framework is actually a large servlet program, which is used to generate objects when the configuration server starts.

The number size determines the order of startup. The smaller the data, the higher the priority.

 

3. default servlet

If the servlet ing path of a servlet is only a forward slash, the servlet becomes the default servlet of the current web application.

If the URL of the matching servlet-mapping element cannot be found in the web and XML files, their access requests will be handled by the default servlet.

In Java, all requests go through the servlet.

Whether it's dynamic, static, images, videos, or something.

For example, if we create a new 1.html Static Page and do nothing, enter http: // localhost: 8080/t_d623/1.html in the browser.

You can find this static page.

This is because the server configures a default servlet path for us.

All server configuration files are in the C: \ Tomcat \ conf folder, and a web. xml file is shared by all programs on the server.

A servlet is registered below, which is started at the server startup.

    <servlet>        <servlet-name>default</servlet-name>        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>        <init-param>            <param-name>debug</param-name>            <param-value>0</param-value>        </init-param>        <init-param>            <param-name>listings</param-name>            <param-value>false</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>


Its registration path is

    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>


All requests that cannot find the correct path follow this path.

When we request a static page, we will use this path request to find whether there is a static page with the corresponding name in the project. If not, we will return

 

Therefore, if we customize a default servlet route in the project, it will replace the server, so be careful.

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.