Application principle of BaseServlet

Application principle of BaseServlet

BaseServlet can call the corresponding method through reflection according to the method parameter, and then judge whether to forward or redirect to the corresponding page according to the result generated by the method.
Combine web.xml with TestServlet to explain the processing of request after inheriting BaseServlet.

http://localhost:8080/MyCrm/testServlet?method=queryUser
The request must have a method parameter. If the form is submitted, you can add an input tag set to hidden mode, and attach a method parameter. The value value of the method is the name of the method to be called in the Servlet that inherits the BaseServlet.

 <servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>servlet.TestServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/testServlet</url-pattern>
    </servlet-mapping>

When the request is intercepted, it will look for a TestServlet class under the servlet package, as follows.

public class TestServlet extends BaseServlet {
    public String queryUser(HttpServletRequest request, HttpServletResponse response){
        String name = "thing";
        //Save to request domain
        request.setAttribute("name",name);
        return "/test.jsp";
    }
}

The request will be processed by the service method. TestServlet has no secondary method, but its parent class BaseServlet overrides the service method, so it will execute the following service method. Let's see what he did.

public class BaseServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String methodName = request.getParameter("method");
        //Get the value value of method, which is the name of the method to be executed. The value obtained here is queryUser. So the method parameter is required.
        Method method = null;
        try {
        //According to the Method name, the class reflection of the parameter gets the Method (Method class)
            method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
        //If there is no such method, a RuntimeException will be thrown.
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("Method you want to call:" + methodName + "It doesn't exist!");
        }

        try {
        //Reflect the execution method, where the result returned by executing the method named queryUse is "/ test.jsp"
            String result = (String)method.invoke(this, request, response);
            //Determine whether the execution result contains': '. In combination with the following, it can be known that the method forwarding and redirecting pages inherited from the BaseServlet to process the request have a fixed format.
            //Forward to f: or no prefix + "/ xxx.jsp"
            //Redirect to r:+"/xxx.jsp"
            if (result != null && !result.trim().isEmpty()) {
                int index = result.indexOf(":");
               //If there is no: "forward to the corresponding page     
                if (index == -1) {
                    request.getRequestDispatcher(result).forward(request, response);
                } else {
                    String start = result.substring(0, index);
                    String path = result.substring(index + 1);
                    //"f:" is forwarded to the corresponding page at the beginning
                    if (start.equals("f")) {
                        request.getRequestDispatcher(path).forward(request, response);
                        //"r:" redirects to the corresponding page at the beginning
                    } else if (start.equals("r")) {
                        response.sendRedirect(request.getContextPath() + path);
                    }
                }
            }

        } catch (Exception var9) {
            throw new RuntimeException(var9);
        }
    }
}

After the above processing, it will be forwarded to the test.jsp page.

<html>
<head>
    <title>test</title>
</head>
<body>
        ${requestScope.name}
        <%--GetrequestDomain name--%>
        <%--The page will display TestServlet Stored "things"--%>
</body>
</html>

Tags: JSP xml

Posted on Thu, 09 Jan 2020 14:05:42 -0500 by kestasjk