Servlet explanation

1. What is a servlet?

Servlet (Server Applet) is the abbreviation of Java Servlet, which is called small service program or service connector. It is a server-side program written in Java, which is independent of platform and protocol. The main function is to browse and generate data interactively. Dynamic web content.
NarrowlyServletRefers to the Java languageAn interface implementedServlet in a broad sense refers to any class that implements this Servlet interface. Under normal circumstances, people understand Servlet as the latter. Servlet runs in an application server that supports Java. In principle, Servlet can respond to any type of request, but in most cases Servlet is only used to extend the Web server based on the HTTP protocol.

2.Servlet implementation process:

The earliest support for Servlet technology was Java Web Server from JavaSoft. Since then, some other Java-based Web Servers began to support the standard Servlet API. The main function of Servlet is to interactively browse and modify data, and generate dynamic Web content. This process is:

1. The client sends a request to the server;
2. The server sends the request information to the Servlet;
3. The Servlet generates the response content and transmits it to the server. The response content is dynamically generated, which usually depends on the client's request;
4. The server returns the response to the client.
Insert picture description here

3. Three ways of servlet realization

1.实现servlet接口

public class ServletDemo1 implements Servlet {
    
    

    //public ServletDemo1(){}

    //生命周期方法:当Servlet第一次被创建对象时执行该方法,该方法在整个生命周期中只执行一次
    public void init(ServletConfig arg0) throws ServletException {
    
    
        System.out.println("=======init方法=========");
    }

    //生命周期方法:对客户端响应的方法,该方法会被执行多次,每次请求该servlet都会执行该方法
    public void service(ServletRequest arg0, ServletResponse arg1)
            throws ServletException, IOException {
    
    
        System.out.println("servlet进来了");

    }

    //生命周期方法:当Servlet被销毁时执行该方法
    public void destroy() {
    
    
        System.out.println("******destroy**********");
    }

    //当停止tomcat时也就销毁的servlet。
    public ServletConfig getServletConfig() {
    
    

        return null;
    }

    public String getServletInfo() {
    
    

        return null;
    }
}

Request http://localhost:8080/web01/demo and found that the int() service() method is output. The
second request only outputs the servlet and the int() method is executed only once.
Insert picture description here
Shut down the shutdown.bat in the tomcat bin directory of the server. Will implement the destroy() method
Insert picture description here
2.继承 GenericServlet 类 一般很少用

public class ServletDemo2 extends GenericServlet {
    
    

    @Override
    public void service(ServletRequest arg0, ServletResponse arg1)
            throws ServletException, IOException {
    
    
        System.out.println("servlet进来了");
    }
}

3、继承 HttpServlet 方法 (常用)

public class ServletDemo3 extends HttpServlet {
    
    
   @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    
    
        System.out.println("servlet进来了");
    }

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

4. The relationship between HttpServlet, GenericServlet, and servlet

For a Servlet class, the most commonly used method in our daily life is to inherit from the HttpServlet class and provide Http-related methods. HttpServlet extends the GenericServlet class, and the GenericServlet class implements the Servlet class and the ServletConfig class.

GenericServlet

GenericServlet is an abstract class that implements the Servlet interface and provides default implementations for init(), destroy() and service(). In GenericServlet, the following tasks are mainly completed:

Assign ServletConfig in init() to a class-level variable, which can be obtained by getServletConfig;
provide default implementations for all methods of Servlet;
directly call methods in ServletConfig;

abstract class GenericServlet implements Servlet,ServletConfig{
    
    
 
   //GenericServlet通过将ServletConfig赋给类级变量
   private trServletConfig servletConfig;
 
   public void init(ServletConfig servletConfig) throws ServletException {
    
    

      this.servletConfig=servletConfig;

      /*自定义init()的原因是:如果子类要初始化必须覆盖父类的init() 而使它无效 这样
       this.servletConfig=servletConfig不起作用 这样就会导致空指针异常 这样如果子类要初始化,
       可以直接覆盖不带参数的init()方法 */
      this.init();
   }
   
   //自定义的init()方法,可以由子类覆盖  
   //init()不是生命周期方法
   public void init(){
    
    
  
   }
 
   //实现service()空方法,并且声明为抽象方法,强制子类必须实现service()方法 
   public abstract void service(ServletRequest request,ServletResponse response) 
     throws ServletException,java.io.IOException{
    
    
   }
 
   //实现空的destroy方法
   public void destroy(){
    
     }
}

HttpServlet

HttpServlet is also an abstract class. It further inherits and encapsulates GenericServlet, making it easier to use. Because it extends the content of Http, it is also necessary to use HttpServletRequest and HttpServletResponse, which are subclasses of ServletRequest and ServletResponse respectively. code show as below:

abstract class HttpServlet extends GenericServlet{
    
    
 
   //HttpServlet中的service()
   protected void service(HttpServletRequest httpServletRequest,
                       HttpServletResponse httpServletResponse){
    
    
        //该方法通过httpServletRequest.getMethod()判断请求类型调用doGet() doPost()
   }
 
   //必须实现父类的service()方法
   public void service(ServletRequest servletRequest,ServletResponse servletResponse){
    
    
      HttpServletRequest request;
      HttpServletResponse response;
      try{
    
    
         request=(HttpServletRequest)servletRequest;
         response=(HttpServletResponse)servletResponse;
      }catch(ClassCastException){
    
    
         throw new ServletException("non-http request or response");
      }
      //调用service()方法
      this.service(request,response);
   }
}

We can see that the methods in the original Servlet are all performed by default in HttpServlet, and there is no need to explicitly destroy initialization and service(). In HttpServlet, a new service() method is customized. The getMethod() method determines the type of request, and then calls doGet() or doPost() to process get and post requests. The user only needs to inherit HttpServlet, and then rewrite the doPost() or doGet() method to process the request.

We generally define a servlet by inheriting HttpServlet.

Guess you like

Origin blog.csdn.net/lirui1212/article/details/104747705