Request forwarding and request inclusion in the Java Web

Source: Internet
Author: User
Tags java web

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.