servlet3.0 first experience

  This is a study note about servlet3.0, sorting and recording is the only way to success. First of all, I will not compare the difference between servlet 3.0 and historical versions. At present, the underlying technologies such as servlet are relatively fixed, and there is not much change between versions.

  Let's talk about servlet first. servlet is a program for Java web server. The main commonly used related jar packages are javax.servlet, which contains the classes and interfaces that define the contract between the servlet and the servlet container. javax.servlet.http which contains the classes and interfaces that define the contract between the httpservlet and the servlet container. The core of servlet technology is servlet, which is an interface that all servlet classes must implement directly or indirectly.

  The life cycle related methods of servlet are init(), service(), destroy().

  init() method

       In the life cycle of a servlet, the init() method will only be executed once, and no matter how many times the user performs requests, this method will not be called. There are two options for the execution timing of the init method. Generally, it is called when the first user requests the servlet after the server starts. You can also set the servlet to execute automatically after the server starts. The init() method is responsible for simply creating or loading some data that will be used throughout the life cycle of the servlet.

  service() method

        When a client requests the servlet, the actual processing work is done by the service() method. The service method is used to process the client's request and generate formatted data to return to the client. 
        Every time a request is made to the server, a new thread will be opened and the service method will be executed. The service will call doGet, doPost and other methods according to the request type of the client. 
        The service is called by the web container. We do not need to do any processing on the specific content of the service. The service will automatically call doGet, doPost and other methods according to the client's request type, so we only need to implement the doGet and doPost methods. .

  destroy() method

        This method will only be called once in the entire life cycle. It is called when the servlet object is destroyed. In the servlet, we can do some operations such as the release of resources. After executing the destroy method, the servlet object will wait for the JVM virtual machine. The garbage collection mechanism is timed to recycle.

  doget(), dopost() methods

        In the actual business processing flow, the service automatically matches which method needs to be executed according to the client's request type.

  

  servlet life cycle

 A servlet is a part of a web server and application server that runs in a servlet container (sometimes called a servlet engine, and is used to provide network services on top of sent requests and responses, decode MIME-based requests, and format MIME-based responses. .Commonly used tomcat, jboss, weblogic are all in the Servlet container), and its life cycle is managed by the container. The servlet life cycle is represented by the init(), Service(), and destroy() methods in the java.servlet.Servlet interface. The life cycle of a servlet has four phases: loading and instantiation, initialization, request processing, and destruction. 

step:

  1. When the servlet container is responsible for loading and instantiating Sevlets. A servlet instance is created when the container starts, or when the container detects that this servlet is needed to respond to the first request. When the container is started, the servlet loads the servlet class through the class loader, and then a new servlet object is completed to complete the instantiation operation. 

  2. After the instantiation operation is completed, the Init() method is called and an object that implements the ServletConfig interface is passed. In the init() method, the servlet can read configuration parameters from the deployment descriptor, or perform any other one-time activity. The init() method is called only once during the entire life cycle of a servlet class.

  3. When the servlet is initialized, the container is ready to process client requests. When the container receives a request for this servlet, it calls the servlet's service() method, passing the request and response objects as parameters. When parallel requests come in, multiple service() methods can run in separate threads at the same time. By analyzing the ServletRequest or HttpServletRequest object, the service() method processes the user's request and calls the ServletResponse or HttpServletResponse object to respond. When the servlet completes a request and responds, it waits for the next request or the server destroys it.

  4. Once the servlet container detects that a servlet is about to be unloaded, either because resources are being reclaimed or because it is being shut down, the container calls the servlet's destroy() method after all servlet service() threads. Then, the servlet can perform garbage collection and cleanup. In this way, the servlet object is destroyed. These four stages together determine the life cycle of a servlet.

  Servlet configuration

      There are two commonly used configuration methods for servlet, one is in the form of annotations, and the other is in the form of web.xml

  1. By way of annotations 

    @WebServlet(name = ("FormServlet"),urlPatterns = {"/formServlet"})
    public class FormServlet extends HttpServlet {}

    The @WebServlet represents the configuration of the servlet through annotations, where the value corresponding to name represents the name of the servlet, and the urlPatterns represents the request path of the servlet. Note that "/" must be added before the request path. If initialization parameters are required then    

    @WebServlet(name = "GenericServletDemoServlet",urlPatterns = {"/enericServletDemo"},
      initParams = {
        @WebInitParam(name = "admin",value = "admin"),
        @WebInitParam(name = "password",value = "admin ")
    }
  )Initialize parameters in this way

  2, through the way of web.xml

  

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>

  <servlet>
    <servlet-name>myServletDemo</servlet-name>
    <servlet-class>com.liu.day02.MyServletDemo</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>myServletDemo</servlet-name>
    <url-pattern>/myServletDemo</url-pattern>
  </servlet-mapping>

</web-app>

  The servlet-name represents the name of the servlet, the servlet-class represents the class corresponding to the servlet, and the url-pattern represents the mapping path.

  The most commonly used servlet configuration is web-based configuration.

   

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325167602&siteId=291194637