How to Implement page Jump in servlet
Client jump
// Use sendredirect of the response object to redirect the client
// Servlet doget Method
Public void doget (https tutorial ervletrequest req, httpservletresponse res)
Throws servletexception, ioexception {
Printwriter out = res. getwriter ();
Out. println ("hello world! ");
Res. sendredirect ("test. do"); // servlet jump (client jump)
}
Client jump cannot pass parameters like the target page (if you need to pass parameters to the target page using this method, you can use the session object to record the parameter values, which are not detailed here)
Server jump
// Use the requestdispatcher interface to redirect the server and pass parameters to the target page
Public void doget (httpservletrequest req, httpservletresponse resp)
Throws servletexception,
Ioexception {
Printwriter out = resp. getwriter ();
/*
* Server jump is implemented in servlet and parameters are passed to the jump page
*/
Req. setattribute ("name", "haiyun"); // Add parameters for the request object
Requestdispatcher dispatcher = req. getrequestdispatcher ("test-04.jsp tutorial"); // get the requestdispatcher object using the req object
Dispatcher. forward (req, resp); // use the requestdispatcher object to jump to the target path on the server side
}