First, the servlet introduction:
With the gradual popularization of internet technology, as well as people's demand for the Internet, the previous static Web page is no longer adaptive, we see today's Web page not only have flash,vide and so on, obviously
The previous static web-page display could not be solved, and in order to solve the problem, Sun offered a technology to solve the problems mentioned above, which is the servlet technology.
The servlet is a technology offered by Sun to develop dynamic Web resources.
In its API, Sun provides a servlet interface that requires the following 2 steps to be completed if users want to use a Dynamic Web resource (that is, to develop a Java program to output data to the browser):
Write a Java class that implements the Servlet interface.
Deploy Java classes that are well developed to a Web server.
Second, the operation of the servlet process:
The servlet program is invoked by the Web server, and the Web server receives the client's servlet access request:
The 1.WEB server first checks to see if the Servlet's instance object has been loaded and created. If it is, perform step 4th directly, otherwise, perform step 2nd.
2. Mount and create an instance object for the servlet.
3. Invokes the Init () method of the Servlet instance object.
Create a HttpServletRequest object to encapsulate the HTTP request message and a HttpServletResponse object that represents the HTTP response message, and then invoke the servlet's service () Method and passes the request and response objects in as parameters.
Before the 4.WEB application is stopped or restarted, the servlet engine (the class that invokes the servlet on the Web server) unloads the servlet and invokes the servlet's Destroy () method before uninstalling.
Three, the lifecycle of the servlet:
1. Speaking of the life cycle, then we have to mention the concept of the cycle, then what is the concept of life cycle?
Life cycle Definition: The life cycle of a thing, when it is born, when it dies, and at some point in its lifetime.
The life cycle of the servlet:
Typically, the server creates an instance object of the Servlet class (the servlet is born) when the servlet is first invoked, and once created, the servlet instance resides in memory for subsequent requests, and the Web container exits. The servlet instance object will be destroyed (the servlet is dead).
During the entire lifecycle of the servlet, the servlet init method is invoked only once when the servlet is created.
Each access request to a servlet causes the servlet engine to invoke a Servlet's service method. For each access request, the Servlet engine creates a new HttpServletRequest request object and a new HttpServletResponse response object.
The two objects are then passed as arguments to the service () method of the servlet that it invokes, and the service method calls the Doxxx method separately, depending on the request. The Destroy () method is invoked before the servlet is destroyed.
Implementation classes for 2.Servlet interfaces:
We know that if we want to implement the Servlet interface, we have to implement all the methods inside, but all of the methods inside are not what we want, then what is the use of implementing this method?
So in order to solve this problem, we generally do not implement the interface, but will inherit the class implementation class, so that we can only implement the method we want to do;
2.1SUN offers a common implementation class for the company:
Servlet Interface Sun Company defines two default implementation classes: Genericservlet, HttpServlet.
HttpServlet refers to the servlet that handles HTTP requests, adding some HTTP protocol processing methods to the original Servlet interface, which is more powerful than the servlet interface. As a result, developers should typically inherit this class when writing a servlet and avoid directly implementing the Servlet interface.
HttpServlet when implementing the Servlet interface, the service method is overridden, and the code inside the method automatically determines how the user is requested, such as a GET request, and calls the HttpServlet Doget method, such as a POST request, to the Dopost method. Therefore, when the developer writes the servlet,
Typically, you only need to override the Doget or Dopost method, rather than overwrite the service method.
Because the client accesses the resources in the Web server through the URL address, the servlet program must map the servlet program to a URL address, using the <servlet> element and < in the Web.xml file, if it wants to be accessed by the outside world. servlet-mapping> element Complete.
The <servlet> element is used to register the servlet, which contains two main child elements:<servlet-name> and <servlet-class> Used to set the registration name of the servlet and the full class name of the servlet, respectively.
A <servlet-mapping> element is used to map an external access path to a registered servlet that contains two child elements:<servlet-name> and <url-pattern> Used to specify the registration name of the servlet and the external access path of the servlet. For example:
Copy Code code 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 of the small details in 2.2Servlet:
Detail 1:
The same servlet can be mapped to multiple URLs, that is, the set value of <servlet-name> child elements of multiple <servlet-mapping> elements can be the registered name of the same servlet.
You can also use the * wildcard character in the URL that the servlet maps to, but there are only two fixed formats: one format is "*. Extension" and the other is preceded by a forward slash (/) and ends with "/*".
Copy Code code 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>
Detail 2:
For some of the following mapping relationships:
Servlet1 Map to/abc/*
Servlet2 Map to/*
Servlet3 Map to/abc
Servlet4 Map to *.do
Problem:
When the request URL is "/abc/a.html", "/abc/*" and "/*" are matched, which Servlet responds
The servlet engine will invoke Servlet1.
When the request URL is "/ABC", both "/abc/*" and "/ABC" match, which Servlet responds
The servlet engine will invoke Servlet3.
When the request URL is "/abc/a.do", both "/abc/*" and "*.do" match, which Servlet responds
The servlet engine will invoke Servlet1.
When the request URL is "/a.do", both "/*" and "*.do" match, which Servlet responds
The servlet engine will invoke Servlet2.
When the request URL is "/xxx/yyy/a.do", both "/*" and "*.do" match, which Servlet responds
The servlet engine will invoke Servlet2.
Detail 3:
If a <load-on-startup> element is configured in the <servlet> element, the Web application loads and creates the instance object of the servlet and the Init () method that invokes the Servlet instance object when it starts.
Example:
Copy Code code 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 a initservlet for Web applications that are configured to load at startup and create the necessary database tables and data for the entire Web application.
Detail 4: Thread-safety issues
When multiple clients concurrently access the same servlet, the Web server creates a thread for each client's access request and invokes the Servlet's service method on this thread. Therefore, if the same resource is accessed within the service method, it is possible to raise thread safety issues.
If a servlet implements the Singlethreadmodel interface, the servlet engine invokes its service method in single-threaded mode. There is no method defined in the
Singlethreadmodel interface, as long as the declaration of implementing the Singlethreadmodel interface is added to the definition of the servlet class. The
Servlet,servlet engine that implements the Singlethreadmodel interface still supports multithreaded concurrent access to the servlet by generating multiple servlet instance objects. Each of the concurrent threads invokes a separate Servlet instance object, respectively. The
implementation of the Singlethreadmodel interface does not really solve the thread-safety problems of the servlet, because the servlet engine creates multiple servlet instance objects. The real problem with multithreading security is that 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).
Four, commonly used objects in the servlet:
1.ServletConfig Object
1.1 in a servlet's configuration file, you can configure some initialization parameters for the servlet using one or more <init-param> tags.
1.2 When the servlet configures the initialization parameters, the Web container automatically encapsulates these initialization parameters into the ServletConfig object when the Servlet instance object is created,
and when invoking the Servlet's Init method, Passes the ServletConfig object to the servlet. In turn, the programmer can get the initialization parameter information for the current servlet
by ServletConfig the object. When the
2.ServletContext object
2.1WEB container starts, it creates a corresponding ServletContext object for each Web application that represents the current Web application. A reference to the ServletContext object is maintained in the
2.2ServletConfig object, and when the developer writes the servlet, The ServletContext object can be obtained by Servletconfig.getservletcontext method.
2.3 Because all the servlet in a Web application shares the same ServletContext object, the Servlet object can communicate through the ServletContext object. The
ServletContext object is often referred to as a context domain object.