[Summary] Servlet development method, Servlet life cycle, url-pattern, Context domain

Three ways to develop Servlet

Method 1: Inherit GenericServlet

public class FirstServlet extends GenericServlet {
    
    

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
    
    
        System.out.println("hello servlet");
    }
}

Note: need to rewrite the service method

Method 2: Inherit HttpServlet

public class SecondServlet extends HttpServlet {
    
    
   
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        
    }
}

Note: There is no need to rewrite the service method, because the HttpServlet class has already rewritten the service method, and the rewritten service method will automatically jump to the doPost or doGet method according to the request method, so these two methods need to be rewritten.

Mode 1 and Mode 2 need to be configured as follows in the web.xml file

    <servlet>
        <servlet-name>first</servlet-name>
        <servlet-class>FirstServlet</servlet-class>
    </servlet>
	<servlet-mapping>
        <servlet-name>first</servlet-name>
        <url-pattern>/first</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>second</servlet-name>
        <servlet-class>SecondServlet</servlet-class>
    </servlet>
	<servlet-mapping>
        <servlet-name>second</servlet-name>
        <url-pattern>/seccond</url-pattern>
    </servlet-mapping>

Method 3: Use @WebServlet annotation

@WebServlet("/servlet3")
public class ThirdServlet extends HttpServlet {
    
    

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    

    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    

    }
}

Servlet life cycle

As the name suggests, it is the whole process of servlet from birth to death.

Three methods are developed in the servlet interface:

  1. init: By default, it is called before the servlet is accessed for the first time.
    The loadOnStartup attribute of the servlet tag defaults to a negative number. If the attribute is changed to a non-negative number, it will be called when the application starts
  2. service: called when the servlet is accessed
  3. destroy: called when the application is uninstalled and the server is shut down

What's the meaning?

  • First of all, the meaning of the life cycle function itself, for init and destroy, if you want to do some extra things when the servlet is created and the servlet is destroyed, you can write the corresponding logic in the init and destroy methods.

What is the meaning of setting init load-on-startup non-negative number?

  • For example, the current servlet needs to do some calculations in advance. If it is executed before the first visit, then the time may be too late. At this time, I can set the init method to execute with the application startup, so there are more Time to calculate.

url-pattern

1. Priority of url-pattern

The url-pattern of the servlet may overlap each other. In the end, which servlet will be called by tomcat to process the request according to the following rules:

  1. The url-pattern beginning with / has a higher priority than the *. suffix
  2. For url-patterns that all start with /, the higher the matching degree (the more matching), the higher the priority

Two, url-pattern details

  1. Can one servlet set multiple url-patterns? can
  2. Can multiple servlets map the same url-pattern? Can't
  3. What are the ways to write servlet url-pattern? /xxx and * . suffixes , all others are illegal.

Three, special url-pattern

  1. / *
    When we set / *, access to the jsp page and access to the static resource page can not be accessed

  2. /
    Default servlet , specifically used to receive requests that no servlet can handle.
    Insert picture description here


Context domain

Used to realize data sharing between multiple servlets

Get the unique context object by calling the getServletContext() method, and call the method on this object:

  1. setAttribute(key,value) save data
  2. getAttribute(key) fetch data
  3. removeAttribute(key) delete data
@WebServlet("/domain1")
public class DomainServlet1 extends HttpServlet {
    
    

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        String name = "zs";
        ServletContext servletContext = getServletContext();
        servletContext.setAttribute("username", name);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    

    }
}
@WebServlet("/domain2")
public class DomainServlet2 extends HttpServlet {
    
    

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        ServletContext servlet = getServletContext();
        String username = (String) servlet.getAttribute("username");
        System.out.println(username);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    

    }
}

Call the getRealPath() method on the context object to get the absolute path of the application root directory

Guess you like

Origin blog.csdn.net/Octavius_/article/details/114242160