JavaWeb Servlet Advanced HttpServlet

Ready to work

  • Introduce servlet-api, jsp-api related jar package or introduce maven dependency
 		<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
            <scope>provided</scope>
        </dependency>

HttpServlet is an abstract class, inherited from GenericServlet, GenericServlet implements the Servlet interface, so the essence is also Servlet, because the implementation of the Servlet interface must implement other four unused methods, it will cause very inconvenient, in order to simplify we only need to implement Our own business service() is enough, so it is more convenient to use the HttpServlet class instead. To use HttpServlet, you only need to inherit and implement the doGet() and doPost() methods.

  • HttpServlet distributes the service() method in Servlet to process requests
  • Http request has GET, POST, PUT, DELETE, etc. request methods
  • There are no other request methods involved in the current Servlet learning, so only the doGet() and doPost() methods need to be implemented.
  • The browser’s default request method is the GET request method.
    As can be seen from the source code in HttpServlet, it is finally distributed and executed through the service() method. Code examples of the oGet() and doPost() methods
    Insert picture description here
@WebServlet("/demo")
public class HelloServlet extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().println("Hello World,这是GET请求");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        resp.getWriter().println("Hello World,这是GET请求");
    }
}

Browser access
Insert picture description here

Analyze the above process through the flowchart
Insert picture description here

Customize a HttpServlet class

Custom MyGenericServlet class

//这个类只帮我们把Servlet接口中的方法都实现掉,而我们只注重service()方法
public  abstract class MyGenericServlet 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() {
    
    

    }




}

Custom MyHttpServlet class

//这个类是将我们的service方法进行优化,将请求分发,根据请求方式来执行特定方法
public class MyHttpServlet extends MyGenericServlet{
    
    
    //只关注业务service方法,其余方法实质性不操作
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
    
    
        //由于浏览器是HTTP请求方式,
        // HttpServletRequest和HttpServletResponse是继承自ServletRequest和ServletResponse
        //则先将先将ServletRequest和ServletResponse转化为HttpServletRequest和HttpServletResponse(向下转型)
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //通过获取浏览器请求方式来进行分发
        String method = request.getMethod();
        //如果是GET请求调用doGet方法
        if (method.equals("GET")) {
    
    
            this.doGet(request, response);
        } else if (method.equals("POST")) {
    
    
            //如果是POST请求调用doPost方法
            this.doPost(request, response);
        }else {
    
    
            //其他请求方式,暂时不处理
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    
    }


}

We write our own Servlet class for business methods

//只专注于GET和POST请求方式即可
@WebServlet("/myServlet")
public class HelloWorldServlet extends MyHttpServlet{
    
    

    //我们自己写的MyHttpServlet类,继承写自己的业务方法
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        response.getWriter().write("Hello World GET");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        response.getWriter().write("Hello World POST");
    }
}

Test example (test by Postman tool)
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45608165/article/details/112336609