Servlet features, servlet implementation, servlet life cycle

1, What is a Servlet

  1. servlet is one of the Java EE specifications, which is the interface

  2. Servlet is one of the three major components of Java web. The three major components are: servlet program, filter and Listener listener.

  3. Servlet is a Java applet running on the server. It can receive the request sent by the client and respond to the data to the client.

2, Manually implement Servlet program

  1. Write a class to implement the Servlet interface

  2. Implement the service method, process the request and respond to the data

  3. To the web Configure the access address of servlet program in xml (the following is the configuration and meaning in xml)

    <!-- servlet Label to Tomcat to configure Servlet program -->
    <servlet>
        <!-- servlet-name label  servlet The program has an alias (usually the class name)-->
        <servlet-name>first</servlet-name>
        <!-- servlet-class yes Servlet Full class name of the program  -->
        <servlet-class>com.example.FirstServlet.HelloServlet</servlet-class>
    </servlet>
    <!-- servlet-mapping Label to servlet Program configuration access address -->
    <servlet-mapping>
        <!-- servlet-name The function of the tag is to tell the server to which my current address is given Servlet Program use-->
        <servlet-name>first</servlet-name>
        <!-- url-pattern Label configuration access address
             / When the server resolves, the address is: http://ip:port / Project path
             /first  Indicates that the address is: http://ip:port / Project path / hello -- >
        <url-pattern>/first</url-pattern>
    </servlet-mapping>
    

How can the browser find the corresponding resources through the address:

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-defalni3-1645100178216)( https://note.youdao.com/yws/res/5782/WEBRESOURCE6da00204cc61d0746aaced1128c096a6 )]

3, Life cycle of Servlet

  1. Execute Servlet constructor method
  2. Execute init method

Step 1 and step 2: during the first access, the Servlet program will call (by default)

On the web The < load on startup > tag is configured in the servlet tag in XML. When the tag value is positive, it is preloaded, the one with larger value is loaded first, and the one with negative tag value is delayed loading. The default value is - 1.

  1. Execute service method

Step 3: every access will call

  1. Execute destroy method

Step 4: call when the web project stops.

4, Three implementation methods of servlet

  1. To implement the servlet interface, you must rewrite five methods

    public class Test implements Servlet {
        @Override
        public void init(ServletConfig servletConfig) throws ServletException {
        }
    
        @Override
        public ServletConfig getServletConfig() {
            return null;
        }
    
        @Override
        public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        }
    
        @Override
        public String getServletInfo() {
            return null;
        }
    
        @Override
        public void destroy() {
        }
    }
    
  2. To inherit the genericServlet abstract class, you must override the service method

    public class Test extends GenericServlet {
    
        @Override
        public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
            
        }
    }
    
  3. Inherit HttpServlet without rewriting any methods. Write doGet() and doPost() methods as needed

    public class Test extends HttpServlet{
    	
    }
    

    tip: the relationship between the three. GenericServlet implements the Servlet interface, and HttpServlet inherits the GenericServlet class.

5, ServletConfig class

Both the Servlet program and the ServletConfig object are created by tomcat,

  1. Three functions of ServletConfig class

    1. Gets the value of the alias Servlet name of the Servlet program

      ​ servletConfig.getServletName();

      String servletName = servletConfig.getServletName();
              System.out.println("servlet Alias for:"+servletName);
      
    2. Get the initialization parameter init param. You can only get your own parameters

      ​ servletConfig.getInitParmeter("username");

      String age = servletConfig.getInitParameter("age");
      System.out.println(age);	
      
    3. Get ServletContext object

      ​ servletConfig.getServletContext();

  2. ServletContext class

    1. What is a ServletContext object

      1. Interface, representing Servlet context object
      2. A web project has only one ServletContext object instance
      3. The ServletContext object is a domain object

      What is a domain object?

      Domain objects, like maps, can access a number of objects

      The domain here refers to the operation range of accessing data

    2. Four functions of ServletContext class

      1. Get web Configure the context parameter context param in XML

         //Get global parameters
         ServletContext context = getServletConfig().getServletContext();
         String username = context.getInitParameter("username");
        
      2. Get the current project path, format: / Project path

        //Get current project path
         ServletContext context = getServletConfig().getServletContext();
         System.out.println(context.getContextPath());
        
      3. Get the absolute path on the server hard disk after project deployment

        //Get the path of project deployment
                /**
                 * /  The address of the inclined rod resolved by the server is http://ip:port/ Project name/
                 */
        ServletContext context = getServletConfig().getServletContext();
        System.out.println(context.getRealPath("/"));
        
      4. Access data like a Map. When the project is destroyed, the data disappears.

        //Get servletContext object
         ServletContext servletContext = getServletContext();
         servletContext.setAttribute("key1","value1");
         System.out.println(servletContext.getAttribute("key1"));
        

Keywords: Java JavaEE

Added by davard on Thu, 17 Feb 2022 15:11:26 +0200