Java-servlet life cycle and JSP

Source: Internet
Author: User

    1. How does the container handle the request resource path?

For example, in the browser address bar type : IP address path /abc.html.

Step1. The default Access is a servlet, which looks for a matching servlet from Web. Xml.
Web. XML Url-pattern

Matching rules :

(1) exact match : <url-pattern>/abc.html</url-pattern>

(2) wildcard match : use the * number to match 0 to multiple characters .
<url-pattern>/*</url-pattern>

(3) Suffix matching : *. The character of the beginning, after the successor meaning
<url-pattern>*.do</url-pattern >
Match all requests ending with. Do

Step2. If there is no matching servlet, find the corresponding file .

    1. Have a servlet handle multiple requests :

A) Use suffix matching , e.g. <url-pattern>*.do</url-pattern >

b) analyze the request resource path and perform different processing According to the results of the analysis.

    1. Servlet declaration Cycle :

(1) What is a servlet 's declaration cycle ?
How the container creates a servlet instance (object), How to initialize it , How to call its methods to process the request , And how to destroy the entire process of its instances ( How the container manages the servlet).

(2) The life cycle is divided into several stages ?
A. instantiation :

1) What is instantiation ?
The container invokes the servlet's constructor to create the corresponding object ;

2) When is the container instantiated ?
2.1) by Default, the container will not create an instance of the request until it is received.
2.2) The container will only create one instance (singleton).
2.3) Additional configuration is required to create an instance of the container immediately after it is started .

B. Initialization

1) What is initialization ?
After instantiation, the container invokes the instance's init method , which is called once .

2) Genericservlet has provided an implementation of the Init method
The ServletConfig object passed over by the container is saved and a getconfig method is provided

3) Initialization parameters
3.1 configuration of calligraphy and painting Parameters (WEN.CML)
3.2 calls the getinitparameter method provided by ServletConfig

4) You can override the init method provided by Genericservlet to implement your own initialization logic , note : it is recommended to overwrite init () instead of init (config)

C. Call ( ready )

1). What is ready ?
After the container receives the request , it calls the service method of the servlet instance to process the request

2). Httpservlet has been implemented service The method.
According to request type class call corresponding Do method Span style= "Font-family:consolas"; such as get request call doget method ,post The request calls the dopost method .

3) You can override the HttpServlet Service method or rewrite the httpservlet doget.dopost method Write processing logic .

D. Destruction

1) What is destroyed .
The container invokes the destroy method of the instance before it deletes the servlet instance (the Destroy method executes only once ).

2) You can override The destroy method provided by Genericservlet to implement your own destruction processing logic .

(3) Several interfaces related to the class ( Understanding ):

    1. Servlet Interface :

Init (servletconfig config)

Service (?,?)

Destroy ()

Note: HttpServletRequest is a Sub-interface of ServletRequest

HttpServletResponse is a Sub-interface of Servletresponse

    1. Genericservlet Abstract class

Implements Some of the methods in the Servlet interface (Init,destroy)

    1. HttpServlet

Inherits the Genericservlet abstract class, mainly implements the service method

    1. JSP (Java Server page)

(1) JSPis what?
Sunthe company developed a server-side dynamicPageTechnical Specifications.
Note: JSPis a. JSPForsuffix of the file,the contents of the document are mainlyHTMLand a small amountof theJavaCode,The container will convert the file into a correspondingservletand then execute(thatJSPis the essence of aservlet).

(2) How to write jsp?
1. Write a file that is suffixed with a . JSP .

2. in this file , you can add the following content :

2.1.html (CSS,JS): Write directly

2.2.java Code :

A.java Code Snippet <% "java code "%>

b.jsp expression <%= "java expression"%>

3. Implied objects:

A. What is an implied object ?
JSP files can be used directly within the object ( such as Out,request,response , etc. )

B. ask what can be used directly ?
the container automatically adds the code to get these objects .

4. Directives:

A. what is an instruction ?
Notifies the container to do some extra processing When translating a JSP file into a servlet class , such as guide packages .

B.page directive :

Import Property : Specifies the package to import , For example : <% @page import= "java.util.Date"% >

(3) How does JSP work ?

Stage a : container first jsp file converted to a corresponding servlet class (. Java file )
HTML ( CSS,JS)---->service method use out.write output
<%  %>----->service method copy.

<%= "java expression" %>---->service in out

Phase two : the container calls the servlet to process the request ( including compilation , instantiation ... ).

Java-servlet life cycle and JSP

Related Article

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.