Servlet technology 6_ Servlet program by inheriting HttpServlet

Generally, in actual project development, the Servlet program is implemented in the form of inheriting the HttpServlet class

step:

  1. Write a class to inherit the HttpServlet class
  2. Rewrite the doGet or doPost method according to business needs
  3. To the access address of the configuration Servlet program in web.xml

HelloServlet2:

package com.servlet1;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HelloServlet2 extends HttpServlet {
    
    
    /**
     * doGet()在get请求的时候调用
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        System.out.println("HelloServlet2的doGet方法");
    }

    /**
     * doPost()在post请求的时候调用
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        System.out.println("HelloServlet2的doPost方法");
    }
}

web.xml configuration file added:

<servlet>
	<servlet-name>HelloServlet2</servlet-name>
	<servlet-class>com.servlet1.HelloServlet2</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>HelloServlet2</servlet-name>
	<url-pattern>/hello2</url-pattern>
</servlet-mapping>

Guess you like

Origin blog.csdn.net/weixin_45024585/article/details/108855392