1. request. getRequestDispatcher () is request forwarding, and a request is shared between the front and back pages;
Response. sendRedirect () is a redirection, and the front and back pages are not a request.
2. RequestDispatcher. forward () is run on the server;
HttpServletResponse. sendRedirect () is accomplished by sending a command to the client browser.
Therefore, RequestDispatcher. forward () is "Transparent" for the browser ";
HttpServletResponse. sendRedirect () is not.
3. The URLs in ServletContext. getRequestDispatcher (String url) can only use absolute paths, while those in ServletRequest. getRequestDispatcher (String url) can use relative paths. Because ServletRequest has the concept of relative paths, while ServletContext does not.
The RequestDispatcher object obtains request requests from the client and passes them to the servlet, html, or jsp on the server. It has two methods:
1. void forward (ServletRequest request, ServletResponse response)
When a request is passed, one Servlet can receive the request, and the other Servlet can use the request to generate the response. Request. response is the information returned by the client. Forward is called before response arrives at the client, that is, before response body output has been flushed. If not, it reports an exception.
2. void include (ServletRequest request, ServletResponse response)
Used to record the request and response. You cannot modify the status information in response later.
To transfer requests to an address in another Web App, follow these steps:
1. Obtain the ServletConext object (currentServletContext. getContext (uripath) of another Web App )).
2. Call the ServletContext. getRequestDispatcher (String url) method.
Eg: ServletContext. getRequestDispatcher ("smserror. jsp"). forward (request, response );
Code example:
Index. jsp:
Copy codeThe Code is as follows:
<% @ 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">
Username: <input type = "text" name = "username"/>
Password: <input type = "password" name = "password"/>
<Input type = "submit"/>
</Form>
Session. java:
Copy codeThe Code is as follows:
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 ");
*/
// This path must be written in this way, rather than using the relative path like the above request. getRequestDispatcher
// If response. sendRedirect is used, the request object cannot be obtained through request. getAttribute in the following session. jsp.
// Because not the same request is used before and after, but the session is acceptable, because the session will always exist 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:
Copy codeThe Code is as follows:
<% @ 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 response. sendRedirect is used, the name and pwd cannot be obtained.
Out. println ("name" + name + "pwd" + pwd );
%>