Servlet's personal understanding, Servlet's personal understanding

Source: Internet
Author: User

Servlet's personal understanding, Servlet's personal understanding

1. What is Servlet?

1. servlet is a dynamic web page technology based on Java technology. It runs on the server side and is managed by Servlet containers to generate dynamic content. It is very complicated for jsp (servlet to develop web pages, so now it is generally used for process control.

Supplement: jsp: the framework of jsp is HTML. It is very simple and convenient to develop web pages. java is nested in it. The essence of jsp is servlet. Jsp Execution Process: jsp ---- translation ---->. java (servlet) ---- compilation ---->. class -----> execution.

2. Servlet is essentially a java class that implements a specific interface. compiling a servlet is actually writing a java class according to the Servlet specification.

3. Servlet is not directly called by users or programmers, but managed by containers. There is no main method.

Ii. Servlet Lifecycle

  

1. Load:

(1) If <load-on-start> is configured in web. xml, it is automatically loaded when the server is started.

(2) loading when the servlet sends a request for the first time without Configuration

2. instantiation:

After the Servlet is loaded, the server creates a Servlet instance.

Supplement: when learning about automatic Spring injection, I wondered why service class injection cannot be performed in servlet. Later, I checked the information and understood the reason: Spring does not manage the servlet. The server will instantiate an object when loading the servlet, if it is in applicationContext. xml, and an object is created. These two objects are not the same!

3. Initialization:

Call the init () method. The Servlet initialization parameter is passed to the Servlet configuration object ServletConfiig.

4. process the request:

Call the service () method, which is used to pass request and response objects for process control. Obtain the request information from the request object (HttpServletRequest), process the request, and return it to the client using the method of the response object (HttpServletResponse. You can also call the doGet () and dopost () methods.

5. Destroy:

When the server does not require a Servlet or reloads a new Servlet instance, the server calls the destroy () method.

3. Servlet-related implementation

-- Servlet must directly or indirectly implement the javax. servlet. Servlet Interface

-- Inherit the javax. servlet. GenericServlet abstract class to implement cross-Protocol Servlet

-- Inherit the javax. servlet. HttpServlet class to implement http Servlet (common method)

    

There are two service methods in it. The following is the parent class. If this is used, you also need to convert requst and response to http. Generally, all the above service methods are directly implemented, or implement the doGet () and doPost () methods (if not implemented, 405 error will be reported

).

4. Configure Servlet in web. xml

<Servlet-name> is used to define the servlet name, which must be unique throughout the application.

<Servlet-class> is used to specify the fully qualified servlet name.

<Servlet-name>: The Servlet name, which is unique and consistent with the name declared in the <servlet> element.

<Url-pattern>: Specifies the URL path relative to the Servlet. This path is relative to the root path of the web application context.

<Servlet-mapping> map the URL mode to a Servlet, that is, the URL processed by the Servlet. That is to say, you can access the servlet through this path.

<Servlet>
<Servlet-name> FristServlet </servlet-name>
<Servlet-class> fully qualified path: FristServlet </servlet-class>
</Servlet>

<Servlet-mapping>
<Servlet-name> FristServlet </servlet-name>
<Url-pattern>/servlet/frist </url-pattern>
</Servlet-mapping>

 

 

 

 

  

 

 

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.