How much do Java Web Programming Servlet Technology know?
1. Servlet basics <br />For Servlet technology development, Sun Company provides some column interfaces and classes, the most important of which is the javax.servlet.Servlet interface. The two important packages are javax.servlet and javax.servlet.http. Servlet is a class that implements the Servlet interface. It is called and created by a web container (Tomcat/Jetty, etc.) to receive and respond to user requests. 5 abstract methods are defined in the Servlet interface:
The top-level class structure of Servlet is as follows:
2. The first Servlet program
Create a new Java Web project, and then create a new Hello class. The Hello.java source code is as follows:
package zzz;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class Hello extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { // Set response message encoding response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("Hello World"); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { this.doGet(request, response); }}The configuration file web.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>zzz</display-name> <servlet> <!-- Register Servlet --> <servlet-name>Hello</servlet-name> <!-- Specify the Servlet name --> <servlet-class>zzz.Hello</servlet-class> <!-- Specify the Servlet full class name --> </servlet> <servlet-mapping> <!-- Map the Servlet external access path --> <servlet-name>Hello</servlet-name> <!-- Specify the Servlet name --> <url-pattern>/hello</url-pattern> <!-- Specify the virtual path to access the Servlet --> </servlet-mapping></web-app>
Starting and running the browser displays as follows:
3. Servlet life cycle
The Servlet life cycle is roughly divided into 3 stages, the initialization stage, the operation stage and the destruction stage . The following are detailed descriptions of each stage:
Initialization phase: When the client makes a request to the Servlet container to access the Servlet, the container first parses the request to check whether the Servlet object is in memory, and if it is used directly; otherwise, create the Servlet object and then call the init() method. Note that init() will only be called once during the entire declaration cycle of the Servlet.
Running stage: This is the most important stage of the Servlet. In this stage, the container will construct a ServletRequest and ServletResponse object as parameters and pass it into the Servlet service() method. For each access of a Servlet, the Servlet container will call the service() method once. The service() method will be called multiple times during the entire life cycle of the Servlet
Destruction stage: When the server is closed or the web application is removed from the container, this stage will be entered. At this time, the destroy() method will be called. During the entire life cycle, the destroy() method will only be called once. When the client first accesses the servlet, the container will create the servlet object, but sometimes it is hoped that the servlet object will be started as soon as Tomcat is started. How to deal with this? At this time, you only need to use the <load-on-startup> element in web.xml. The example configuration is as follows:
<servlet> <servlet-name>Hello</servlet-name> <servlet-class>zzz.Hello</servlet-class> <!-- Automatic loading of servlet program--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
4. ServletConfig and ServletContext
During the ServletConfig interface , if some auxiliary information is needed, such as file encoding, companies using Servlets, etc., this information can be configured using the <init-param> element in web.xml. When Tomcat initializes a Servlet, the Servlet configuration information will be encapsulated into a ServletConfig object and passed to the Servlet through init (ServletConfig config). ServletConfig defines a series of methods to obtain configuration information:
Next, take getInitParameter() as an example to explain the use of this method:
The web.xml configuration file is as follows:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>zzz</display-name> <servlet> <servlet-name>Hello</servlet-name> <servlet-class>zzz.Hello</servlet-class> <init-param> <param-name>company</param-name> <param-value>dahua</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping></web-app>
Hello.java file is as follows:
package zzz;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletConfig;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class Hello extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { // Set response message encoding response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); ServletConfig config = this.getServletConfig(); String param = config.getInitParameter("company"); out.println("company: " + param); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { this.doGet(request, response); }} ServletContext interface
When the Servlet container is started, a unique ServletContext object is created for each web application to represent the current web application. This object not only encapsulates all information of the web application, but also implements data sharing among multiple Servlet objects. In web.xml, you can not only configure the initialization information of the servlet, but also configure the initialization information of the entire web application. The configuration method is as follows:
<context-param> <param-name>name</param-name> <param-value>luoxn28</param-value></context-param><context-param> <param-name>company</param-name> <param-value>dahua</param-value></context-param>
Note: There can only be one <param-name> and <param-value> in a <context-param> element, and the above configuration is configured under <web-app> in web.xml. Hello.java file is as follows:
package zzz;import java.io.IOException;import java.io.PrintWriter;import java.util.Enumeration;import javax.servlet.ServletContext;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class Hello extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { // Set response message encoding response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); ServletContext context = this.getServletContext(); Enumeration<String> paramNames = context.getInitParameterNames(); while (paramNames.hasMoreElements()) { String name = paramNames.nextElement(); String value = context.getInitParameter(name); out.println(name + ": " + value + "<br/>"); } } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { this.doGet(request, response); }}Since all Servlets in a web application share a ServletContext object, the domain properties of the ServletContext object can be accessed by all Servlets in the web application. In the ServletContext interface, four methods are defined to add, delete and set ServletContext domain properties:
Through the above method, you can realize the sharing of data between multiple servlets. This specific example will not be posted with code. There are many information that can be referenced online, such as clicking:
Javaweb Servlet Development Summary (II)
The above is all about this article, and I hope it will be helpful for everyone to understand the Servlet technology of Java Web.
References
1. "Introduction to Java Web Program Development" Servlet Technology Chapter