Detailed description of Servlet dynamic web page technology

Source: Internet
Author: User

1. Servlet introduction:
With the gradual popularization of Internet technologies and the increasing requirements of the Internet, the previous static Web pages are no longer suitable. We can see that today's web pages not only have flash, vide, and so on, obviously
Previously, static Web page display could not be solved. To solve this problem, SUN provided a technology to solve the above problems, which is Servlet technology.
Servlet is a technology provided by sun for developing dynamic web resources.
Sun provides a servlet interface in its API. To send a dynamic web Resource (that is, to develop a Java program to output data to the browser), follow these two steps:
Compile a Java class to implement the servlet interface.
Deploy the developed Java class to the web server.

Ii. Servlet running process:
The Servlet program is called by the WEB server. After the web server receives the Servlet access request from the client:
1. The Web server first checks whether the Servlet instance object has been loaded and created. If yes, go to step 1. Otherwise, go to step 2.
2. Load and create an instance object for the Servlet.
3. Call the init () method of the Servlet instance object.
Create an HttpServletRequest object that encapsulates the HTTP request message and an HttpServletResponse object that represents the HTTP Response Message. Then call the Servlet service () method and pass the request and response object as parameters.
4. Before the WEB application is stopped or restarted, the Servlet Engine (the class that calls the Servlet in the WEB server) Will uninstall the Servlet and call the Servlet's destroy () method before uninstalling it.

3. Servlet lifecycle:
1. Speaking of the lifecycle, we have to mention the concept of the lifecycle. What is the concept of the lifecycle?
Lifecycle definition: refers to the lifecycle of a thing, when it is generated, when it dies, and when it is triggered at a certain time point in its survival stage.
Servlet lifecycle:
Generally, the server creates the Instance Object of the Servlet class when the Servlet is called for the first time (the servlet was born). Once created, the Servlet instance will reside in the memory, service for subsequent requests; servlet instance objects will not be destroyed until the web Container exits (servlet dead ).
During the entire Servlet lifecycle, the Servlet init method is called only once when the servlet is created.
Each access request to a Servlet causes the Servlet engine to call the servlet service method once. For each access request, the Servlet engine creates a new HttpServletRequest request object and a new HttpServletResponse response object,
Then, the two objects are passed as parameters to the Servlet service () method called by the Servlet. The service method then calls the doXXX method according to the request method. The destroy () method is called before the servlet is destroyed.

2. Servlet interface implementation class:
We know that if we want to implement the Servlet interface, we must implement all the methods in it. However, all the methods in it are not what we want. At this time, how can we implement this method?
To solve this problem, we generally do not implement this interface, but inherit the implementation class of this class, so we only need to implement the method we want;

2.1SUN provides common implementation classes:
Servlet interface SUN defines two default implementation classes: GenericServlet and HttpServlet.

HttpServlet is a servlet that can process HTTP requests. It adds some HTTP protocol processing methods to the original Servlet interface. It is more powerful than the Servlet interface. Therefore, when writing a Servlet, developers should generally inherit this class, instead of directly implementing the Servlet interface.

When HttpServlet implements the Servlet interface, it overwrites the service method. The code in this method automatically determines the user's request method. For example, if it is a GET request, it calls the doGet method of HttpServlet, for a Post request, the doPost method is called. Therefore, when developers write Servlets,
Generally, you only need to override the doGet or doPost method, instead of overwriting the service method.

Because the client accesses resources on the web server through the URL address, if the Servlet program is to be accessed by the outside world, the servlet program must be mapped to a URL address, which works on the web. use the <servlet> element and <servlet-mapping> element in the xml file.
The <servlet> element is used to register a Servlet. It contains two main sub-elements: <servlet-name> and <servlet-class>, set the Registration Name of the Servlet and the complete Class Name of the Servlet respectively.
A <servlet-mapping> element is used to map an external access path of a registered Servlet. It contains two sub-elements: <servlet-name> and <url-pattern>, it is used to specify the Registration Name of the Servlet and the external access path of the Servlet respectively. For example:

Copy codeThe Code is as follows:
<Servlet>
<Servlet-name> servlet3 </servlet-name>
<Servlet-class> cn. baidu. serlvet. Demo3Servlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> servlet3 </servlet-name>
<Url-pattern>/demo3 </url-pattern>
</Servlet-mapping>

Some small details in 2.2Servlet:
Details 1:
The same Servlet can be mapped to multiple URLs, that is, the value of the <servlet-name> sub-element of multiple <servlet-mapping> elements can be the Registration name of the same Servlet.
* Wildcard can also be used in the URL mapped to the Servlet, but there are only two fixed formats: one is "*. extension. The other format starts with a forward slash (/) and ends with a slash.

Copy codeThe Code is as follows:
<Servlet-mapping>
<Servlet-name>
AnyName
</Servlet-name>
<Url-pattern>
*. Do
</Url-pattern>
</Servlet-mapping>
<Servlet-mapping>
<Servlet-name>
AnyName
</Servlet-name>
<Url-pattern>
/Action /*
</Url-pattern>
</Servlet-mapping>

Details 2:
For the following mappings:
Serving Servlet1 to/abc /*
Serving Servlet2 /*
Serving Servlet3 to/abc
Serving Servlet4 to *. do
Problem:
When the request URL is "/abc/a.html", "/abc/*" and "/*" match, which servlet response
The Servlet Engine calls Servlet1.
When the request URL is "/abc", "/abc/*" and "/abc" Both match and the servlet response
The Servlet Engine calls Servlet3.
When the request URL is "/abc/a. do", "/abc/*" and "*. do" Both match and the servlet response
The Servlet Engine calls Servlet1.
When the request URL is "/a. do", "/*" and "*. do" Both match and which servlet responds
The Servlet Engine calls Servlet2.
When the request URL is "/xxx/yyy/a. do", "/*" and "*. do" Both match and the servlet response
The Servlet Engine calls Servlet2.
Details 3:
If a <load-on-startup> element is configured in the <servlet> element, when a WEB application is started, the instance object of the Servlet will be loaded and created, and the init () method of the Servlet instance object will be called.
Example:
Copy codeThe Code is as follows:
<Servlet>
<Servlet-name> invoker </servlet-name>
<Servlet-class>
Org. apache. catalina. servlets. InvokerServlet
</Servlet-class>
<Load-on-startup> 2 </load-on-startup>
</Servlet>

Purpose: Write an InitServlet for a web application. This servlet is configured to start the loading and create necessary database tables and data for the entire web application.

Details 4: thread security issues
When multiple clients concurrently access the same Servlet, the web server creates a thread for each client's access request and calls the Servlet service method on this thread, therefore, if the same resource is accessed in the service method, thread security problems may occur.
If a Servlet implements the SingleThreadModel interface, the Servlet engine calls its service method in single-threaded mode.
No method is defined in the SingleThreadModel interface. You only need to add a Declaration to implement the SingleThreadModel interface in the Servlet class definition.
For servlets that implement the SingleThreadModel interface, the Servlet engine still supports multi-threaded concurrent access to the Servlet. The method used is to generate multiple Servlet instance objects, each concurrent thread calls an independent Servlet instance object.
Implementing the SingleThreadModel interface does not really solve the Servlet thread security problem, because the Servlet engine creates multiple Servlet instance objects, to solve the multi-thread security problem, a Servlet instance object is called by multiple threads at the same time.
In fact, in Servlet API 2.4, SingleThreadModel has been marked as Deprecated (obsolete ).

4. Common objects in Servlet:
1. ServletConfig object
1.1 In the Servlet configuration file, you can use one or more <init-param> labels to configure some initialization parameters for the servlet.
1.2 When the servlet configures initialization parameters, the web Container automatically encapsulates these initialization parameters into the ServletConfig object when creating the servlet instance object,
When the servlet init method is called, The ServletConfig object is passed to the servlet. Then, the programmer can get the current servlet through the ServletConfig object.
.
2. ServletContext object
2.1when a WEB container is started, it creates a corresponding ServletContext object for each web application, which represents the current WEB application.
2.2ServletConfig maintains the reference of the ServletContext object. When writing a servlet, developers can obtain the ServletContext object through the ServletConfig. getServletContext method.
2.3 because all servlets in a WEB application share the same ServletContext object, Servlet objects can communicate with each other through ServletContext objects.
ServletContext objects are also called context domain objects.

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.