1.request.getrequestdispatcher () is a request forwarding, front and back page sharing a request;
Response.sendredirect () is redirected, and the front and back pages are not a request.
2.requestdispatcher.forward () is running on the server side;
Httpservletresponse.sendredirect () is done by sending commands to the client browser.
So Requestdispatcher.forward () is "Transparent" to the browser;
and Httpservletresponse.sendredirect () is not.
The URL in 3.servletcontext.getrequestdispatcher (string URL) can only use absolute paths; servletrequest.getrequestdispatcher (string URL) The URL in can use a relative path. Because ServletRequest has the concept of relative path, and ServletContext object has no secondary concept.
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)
Used to pass request, a servlet can receive request requests, and another servlet uses this request to generate response. Request, response is the information returned by the client. Forward to be called before response arrives at the client, which is before response body output has been. If not, it will report an exception.
2.void include (servletrequest request,servletresponse response)
Used to record reservations and response, you can no longer modify the status of response in the information.
If you need to transfer a request to an address in another Web app, you can do the following:
1. Get another web app's Servletconext object (Currentservletcontext.getcontext (Uripath)).
2. Invoke the Servletcontext.getrequestdispatcher (String url) method.
Eg:ServletContext.getRequestDispatcher ("smserror.jsp"). Forward (Request,response);
Code instance:
INDEX.JSP:
Copy Code code 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 are my page"/>
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >
-->
<form action= "Servlet/session" method= "POST" >
User name: <input type= "text" name= "username"/>
Password: <input type= "password" name= "password"/>
<input type= "Submit"/>
</form>
Session.java:
Copy Code code 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 is called when a form has it 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 is called when a form, has its tag value, 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 so that the relative path cannot be used like the one above Request.getrequestdispatcher
And if you use Response.sendredirect, you can't get the request by Request.getattribute in the session.jsp below.
This is not the same request, but the session can be used, because sessions 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 Code code 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 are 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 you use Response.sendredirect above, you cannot get the name and pwd
Out.println ("name" + name + "pwd" + pwd);
%>