Jump between servlets

Back to home

This is just a simple understanding, because now the mainstream development mode can hardly use the Servlet, or it only comes with the framework, so it's packaged.

Servlet s can jump to each other, so that multiple servlets can be used for modularization.

Servlet jump is used in MVC (model view control) framework. The MVC framework divides the program into three independent modules:

  • Business processing module
  • View module
  • control module
    For example, the three parts of the Struts framework are three servlets, and the program jumps between them.

1. Turn to forward

The transition to forward is implemented through the forward method of the RequestDispatcher object.

    req.setAttribute("errorResult", "File does not exist download failed");
    RequestDispatcher dispatcher = req.getRequestDispatcher("/servlet/AServlet");
    dispatcher.forward(req, resp);

/Represents the root of the Web application
Forward is a common technology in MVC framework. Forword can not only jump to another Servlet and JSP page of this application, but also to another file, or even the file under the WEB-INF file.

No more examples, if order is useful to see.

This jump is transparent to the client, because it completes, carries or processes req and resp on the server.
Forward is the most commonly used method. In MVC frameworks such as Struts and WebWork, servlets are used to process user requests. The results are put into the request through request.setAttribute(), and then forward is displayed in JSP.

Before using forward, no input can be input to the client, that is, out.println() cannot be used, otherwise an exception IllegalStateException will be thrown

2. Redirect

Redirection is achieved by the status code returned by the server.
When the client browser requests the server, the server will return a status code. The server sets the status code through the SetStatus method of the HTTP servletresponse.
If the server returns 301 or 302, the browser will re request the resource at the new web address.


Change the calculation of individual income tax

  protected void doPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, IOException {

    this.log("implement doPost() Method..................");
    if(true){
      resp.setStatus(301);
      resp.setHeader("Location","/FirstWeb/Test.html");
      return;
    }

Location can also point to a specific website, for example:
https://www.baidu.com/

  protected void doPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, IOException {

    this.log("implement doPost() Method..................");
    if(true){
//      resp.setStatus(301);
//      resp.setHeader("Location","/FirstWeb/Test.html");
      resp.sendRedirect("/FirstWeb/Test.html");
    }

In fact, using the method encapsulated above, sendRedirect sets 301 and the target address to jump.

F12 can see that the request is initiated by the server side, and the jump can see the request twice.

3. Auto refresh

  protected void doPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, IOException {

    this.log("implement doPost() Method..................");
    resp.setHeader("Refresh","3;url=http://localhost:10024/FirstWeb/Test.html");

    resp.setCharacterEncoding("UTF-8");
    req.setCharacterEncoding("UTF-8");
    resp.setContentType("text/html");

The unit of Refresh is seconds, which can be used in some stock software, right-click, agent and other pages.
In fact, it is to set the Refresh attribute, which f12 can see.
In addition to the background settings, the foreground page can also be set through the properties.
However, the project is not playing like this any more. It uses js+ajax to call the background rest service regularly to get json data.

Back to home

164 original articles published, praised 11, visited 8193
Private letter follow

Tags: Struts JSP Attribute REST

Posted on Sun, 09 Feb 2020 07:20:20 -0800 by pets2soul