Forwarding in the ServletContext

Source: Internet
Author: User

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

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.