1. Across multiple servlets in one request
2. Multiple servlets in one request, they share the request object. is to setattribute () save data in the Aservle data in Bservlet by GetAttribute ()
3, in the Aservlet forward to Bservlet, then the entire request is bservlet to complete, aservlet no longer intervene
4, Aservlet contains Bservlet, then two servlets together to complete the request
5. Request forwarding and request inclusion require the use of an object RequestDispatcher.
Aservlet.java
PackageCn.edu.aynu.rjxy.servlet;Importjava.io.IOException;ImportJava.io.PrintWriter;ImportJavax.servlet.RequestDispatcher;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classAservletextendsHttpServlet {/** Request Forwarding **/ Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//Setting the response headerResponse.setcontenttype ("Text/html;charset=utf-8"); Response.getwriter (). Print ("Aservlet ..."); Request.setattribute ("Name", "Zhang San"); //get RequestDispatcher ObjectRequestDispatcher rd = Request.getrequestdispatcher ("/bservlet"); //passing the Request object and the response object to the BservletRd.forward (request, response); }}
Bservlet.java
PackageCn.edu.aynu.rjxy.servlet;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classBservletextendsHttpServlet { Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {String name= (String) request.getattribute ("name"); Response.getwriter (). Print ("Bservlet ..."); Response.getwriter (). Print ("Name=" +name); }}
Cservlet.java
PackageCn.edu.aynu.rjxy.servlet;Importjava.io.IOException;ImportJava.io.PrintWriter;ImportJavax.servlet.RequestDispatcher;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classCservletextendsHttpServlet {/** Request contains **/ Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//Setting the response headerResponse.setcontenttype ("Text/html;charset=utf-8"); Response.getwriter (). Print ("Aservlet ..."); RequestDispatcher Rd= Request.getrequestdispatcher ("/dservlet"); Rd.include (request, response); }}
Dsevler.java
PackageCn.edu.aynu.rjxy.servlet;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classDservletextendsHttpServlet { Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.getwriter (). Print ("Dservlet ..."); }}
1, request forwarding is a request, and redirection is two requests
2, the browser address bar will not change after the request is forwarded the redirect changes
3. The target of request forwarding can only be in the resources of this project, the target of redirection may be other application
4, request forwarding to Aservlet and Bservlet request method is the same, is either all get, or both are post. Because they are the same request
5. Request forwarding can share the request data in multiple servlets, and redirection is not possible.
Request forwarding and request inclusion in the Java Web