Detailed explanation of Servlet Technology of Java Web Programming

How much do you know about Servlet Technology of Java Web programming?

1. Servlet foundation for the development of Servlet Technology, Sun company provides some column interfaces and classes, the most important of which is javax servlet. Servlet interface, 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 the web container (Tomcat / jetty, etc.) to receive and respond to user requests. Five abstract methods are defined in the servlet interface:

The structure of servlet top-level class is as follows:

2. First servlet program

Create a new Java Web project, and then create a new Hello class, hello The Java source code is as follows:

Configuration file web The XML is as follows:

Start and run the browser as follows:

3. Servlet lifecycle

The servlet life cycle is roughly divided into three stages: initialization stage, operation stage and destruction stage. The following is a detailed description of each stage:

Initialization phase: when the client sends a request to the servlet container to access the servlet, the container first parses the request to see if there is the servlet object in memory. If so, it can be used directly; Otherwise, create the Servlet object and then call the init () method. Note that in the whole Servlet declaration cycle, init () will only invoke a run phase: This is the most important stage of Servlet, which will construct a ServletRequest and ServletResponse object as a service () method for passing parameters to Servlet. For each access to the servlet, the servlet container will call the service () method once, and the service () method will be called multiple destruction stages in the whole life cycle of the servlet: when the server is shut down or the web application is removed from the container, it will enter this stage, and the destroy () method will be called, The destroy () method will only be called once in the whole life cycle. When the client accesses the servlet for the first time, the container will create the servlet object, but sometimes you want the servlet object to follow the startup when Tomcat starts. How to deal with this? At this point, you only need to The < load on startup > element can be used in XML. The example configuration is as follows:

4. ServletConfig and ServletContext

ServletConfig interface during servlet operation, if you need some auxiliary information, such as file code, company using servlet, etc., these information can be displayed on the web The < init param > element is used for configuration in 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 example of this method:

web. The XML configuration file is as follows:

Hello. The java file is as follows:

ServletContext interface when the servlet container is started, a unique ServletContext object will be created for each web application to represent the current web application. This object not only encapsulates all the information of the web application, but also realizes data sharing among multiple servlet objects. On the web XML can configure not only the initialization information of servlet, but also the initialization information of the whole web application. The configuration method is as follows:

Note: there can only be one < param name > and < param value > in a < context param > element. The above configuration is on the web XML under < web app >. Hello. The java file is as follows:

Since all servlets in a web application share a ServletContext object, the domain attribute 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 attributes respectively:

Through the above methods, data sharing among multiple servlets can be realized. This specific example does not post code. There are many materials available for reference on the Internet. For example, you can click:

Summary of Java Web servlet development (2)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>