The client sends a request to the server, the server forwards the request, gets the response information, the client sends only one request, and the Address bar information does not change.
The server receives the class and forwards it
Package Com.itheima.zhuanfa;import Java.io.ioexception;import Javax.servlet.requestdispatcher;import Javax.servlet.servletcontext;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public Class SERVLETCONTEXTDEMO3 extends httpservlet{@Overrideprotected void doget (HttpServletRequest req, httpservletresponse RESP) throws Servletexception, IOException {//forwards//Get ServletContext Object ServletContext sc=getservletcontext (); System.out.println ("Pre-forwarding"); Resp.getoutputstream (). Write ("before". GetBytes ());//Get Transponder RequestDispatcher rd= Sc.getrequestdispatcher ("/demo4");//Get Request Transponder Rd.forward (REQ,RESP);/* Forwarding: The requested Address bar is the same, both share the request and the Response object * Before and after forwarding the page output will not be sent to the client * before forwarding do not refresh the contents of the response, response content in the empty */system.out.println ("forward"); Resp.getoutputstream (). Write ("after". GetBytes ());} @Overrideprotected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException { }}
Forwarded to the class to upload this class of information to the client
Package Com.itheima.zhuanfa;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class ServletContextDemo4 extends HttpServlet {@Overrideprotected void Doget (httpservletrequest req, HttpServletResponse resp) throws Servletexception, IOException {resp.getoutputstream (). Write ("Forwarding succeeded, I came from Demo4". GetBytes ());} @Overrideprotected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException { }}
Forwarding in the ServletContext