Learning servlet (Declaration: Most materials come from the Internet)

Source: Internet
Author: User

Recently, I want to prepare a servlet modification project. I just took this opportunity to review the servlet. I 've been using it for many years and see how much I can think.
Conclusion :※※※※JSP is Servlet※※※※

 

Servlet name: SERVER + applet = servlet indicates the server-side Applet

JSP is the Java Server Page.

 

In fact, the Java Virtual Machine does not know such a thing as JSP. When JSP is used, it will be converted into servlet by the container and then called.

You wentYou can find the file with the same name as your project in the work \ Catalina \ localhost \ folder under the Tomcat directory. here you have the servlet. Java and class files converted from JSP.

Servlet is a server-side applet, which was generated earlier than JSP. later it was found that it was difficult to directly output the user's web interface in servlet, and JSP was born. It is used to assist servlet in displaying web pages, it is used as the presentation layer of web development. In fact, JSP is first compiled as a servlet on the server side (usually when the customer visits it for the first time). Therefore, it is actually a special servlet, but it is generally used for page display.

 

My understanding is that JSP is more like a template, and many things are fixed. Programmers only need to pay attention to the Java code implementation.

Servlet does not have a template, and its output relies entirely on Java code. Even the "<tr>" "<TD>" tag on the page must be manually written to the Java code.

 

Servlet Lifecycle

Load servlet. This operation is generally executed dynamically. However, the server usually provides a management option for force loading and initializing specific servlets at server startup.

Server creates a servlet instance

The server calls the servlet Init () method.

A client request arrives at the server

Server creates a request object

Server creates a response object

The server activates the servlet Service () method and transmits the request and response object as the parameter.

The Service () method obtains information about the request object, processes the request, accesses other resources, and obtains the required information.

The Service () method uses the response object method to send the response back to the server and finally reach the client. The Service () method may activate other methods to process requests, such as doget () or dopost (), or a new method developed by the programmer.

For more client requests, the server creates a new request and response object, still activates the Service () method of the servlet, and passes these two objects as parameters to it. Repeat the above loop, but you do not need to call the init () method again. Generally, the servlet is initialized only once (Only one object) When the server no longer needs servlet (usually when the server is shut down), the server calls the Servlet's destroy () method.

 

Servlet Working Mode

The client sends a request to the server.

The server starts and calls the servlet. The servlet generates the response content based on the client request and sends it to the server.

The server returns the response to the client.

 

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.