Servlet is a technology provided by Sun for developing dynamic web resources.
Sun provides a servlet interface in its API. To develop 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.
Quick Start: Use servlet to output "Hello servlet" to the browser ". Read the servlet API to solve two problems:
- Which method should I write the Java code that outputs Hello servlet in?
Public voidService(Servletrequest req, servletresponse res)
- How to output data to IE browser?
Response. getwriter (). Write ();
The servlet program is called by the Web server. After the Web server receives the servlet access request from the client:
- The Web server first checks whether the servlet instance object has been loaded and created. If yes, perform Step 4. Otherwise, perform step 2.
- Load and create an instance object for this servlet.
- 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.
- Before the web application is stopped or restarted, the servlet engine uninstalls the servlet and calls the Servlet's destroy () method before uninstalling the servlet.
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 will create a new httpservletrequest request object and a new httpservletresponse response object, and then pass these two objects as parameters to the servlet Service () It calls () the service method then calls the doxxx Method Based on the Request Method.
- The destroy () method is called before the servlet is destroyed.
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 writing a servlet, developers generally only need to override the doget or dopost method, rather than the service method.
Servlet details
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:
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.
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. Which servlet response?
The Servlet Engine calls servlet3.
When the request URL is "/ABC/a. Do", "/ABC/*" and "*. Do" Both match. Which servlet response?
The Servlet Engine calls servlet1.
When the request URL is "/a. Do", "/*" and "*. Do" Both match. Which servlet response?
The Servlet Engine calls servlet2.
When the request URL is "/XXX/YYY/a. Do", "/*" and "*. Do" Both match. Which servlet response?
The Servlet Engine calls servlet2.
Servlet is a Java class called by other Java programs (Servlet Engine). It cannot run independently. Its running is controlled and scheduled by the servlet engine.
For multiple servlet requests from the client, the server usually creates only one servlet instance object. That is to say, once the servlet instance object is created, it will reside in the memory, for other subsequent request services, the servlet instance object will not be destroyed until the Web Container exits.
During the entire servlet lifecycle, the servlet init method is called only once. Each access request to a servlet causes the servlet engine to call the servlet service method once. For each access request, the servlet engine will create a new httpservletrequest request object and a new httpservletresponse response object, and then pass these two objects as parameters to the servlet Service () It calls () the service method then calls the doxxx Method Based on the Request Method.
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.
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.
If the servlet ing path of a servlet is only a forward slash (/), the servlet becomes the default servlet of the current web application.
All in the web. the URL of the matching <servlet-mapping> element cannot be found in the XML file. All access requests are sent to the default servlet for processing. That is to say, the default servlet is used to process access requests that are not processed by other servlets.
In the <tomcat installation directory> \ conf \ Web. an XML file named Org. apache. catalina. servlets. servlet ultservlet Servlet and set this servlet as the default servlet.
When accessing a static html file and image on the Tomcat server, the default servlet is actually accessed.
Thread Security
- 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 ).
Servletconfig object
- In the servlet configuration file, you can use one or more <init-param> labels to configure some initialization parameters for the servlet.
- After the servlet configures initialization parameters, the Web Container automatically encapsulates the initialization parameters in the servletconfig object when creating the servlet instance object. When the servlet init method is called, pass the servletconfig object to the servlet. Then, the programmer can get the initialization parameter information of the current servlet through the servletconfig object.
Servletcontext object
- When a Web container is started, it creates a corresponding servletcontext object for each web application, which represents the current web application.
- The servletconfig object maintains the reference of the servletcontext object. When writing a servlet, developers can obtain the servletcontext object through the servletconfig. getservletcontext method.
- 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.
Servletcontext Application
- Multiple servlets share data through the servletcontext object.
- Obtain the initialization parameters of the Web application.
- Implement servlet forwarding.
- Use the servletcontext object to read resource files.
Get File Path
Three methods for reading resource files
Getting started with Servlet Development