jsp jump getRequestDispatcher of and sendRedirect of difference

  • 2021-01-14 06:25:13
  • OfStack

1. request. getRequestDispatcher() is a request forwarding, and the front and rear pages share 1 request;
response.sendRedirect () is a redirection, the page before and after is not 1 request.

2. RequestDispatcher.forward () is run on the server;
HttpServletResponse.sendRedirect () is done by sending a command to the client's browser.
So RequestDispatcher.forward () is "transparent" to the browser;
HttpServletResponse.sendRedirect () is not.

3.ServletContext.getRequestDispatcher(String url) url can only use absolute paths; url in ServletRequest.getRequestDispatcher(String url) can use relative paths. Because ServletRequest has the concept of relative paths; The ES34en object has no sub-concept.

The RequestDispatcher object takes requests for request from the client and passes them on to servlet,html, or jsp on the server. It has two methods:

1.void forward(ServletRequest request,ServletResponse response)

For request requests, one Servlet receives the request request, and another Servlet uses the request request to generate the response. Request passed by request, and response is the information returned by the client. forward is called before response reaches the client, that is, before response body output has been flushed. If not, it will raise an exception.

2.void include(ServletRequest request,ServletResponse response)

It is used to keep request and response records. The status information in response cannot be modified in the future.

If a request needs to be routed to an address in another Web App, do the following:
1. Get another Web object (currentServletContext.getContext(uripath)).

2. Call ServletContext.getRequestDispatcher(String url).

eg: ServletContext getRequestDispatcher (" smserror. jsp "). forward (request response);

Code example:
index. jsp:


<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

My JSP 'index.jsp' starting page
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
<meta http-equiv="description" content="This is my page" />
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
    -->
<form action="servlet/session" method="post">
   The user name :<input type="text" name="username" />

   password :<input type="password" name="password" />

  <input type="submit" />
  </form>

session. java:


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class session extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public session() {
        super();
    }

    /**
     * Destruction of the servlet.

     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet.

     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * The doPost method of the servlet.

     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = "";
        String password = "";
        username = request.getParameter("username");
        password = request.getParameter("password");
        HttpSession session = request.getSession();
        session.setAttribute("username", username);
        session.setAttribute("password", password);
        request.setAttribute("name", username);
        request.setAttribute("pwd", password);

        RequestDispatcher dis = request.getRequestDispatcher("/getsession.jsp");
        dis.forward(request, response);

        /*
        response.sendRedirect("http://localhost:8080/sessiontest/getsession.jsp");
        */
                // The path has to be written like this, not like this request.getRequestDispatcher That uses relative paths 
                //   And if you use response.sendRedirect The words are below session.jsp Cannot pass in request.getAttribute In order to get request object 
                // Because it's not the same thing 1 a request , but session Yes, because session will 1 It exists until the user closes the browser 
    }

    /**
     * Initialization of the servlet.

     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

getsession. jsp:


<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

My JSP 'getsession.jsp' starting page

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
<meta http-equiv="description" content="This is my page" />
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  <%   out.print("");   String username = (String)session.getAttribute("username");  
String password = (String)session.getAttribute("password");  
String name = (String)request.getAttribute("name");  
String pwd = (String)request.getAttribute("pwd"); 
 out.println("username " + username + " password " +password);
 // If the above is used response.sendRedirect You can't get it name and pwd  
 out.println("name " + name + "pwd "+ pwd);   
  %>


Related articles: