Request forwarding and redirection in a servlet

Source: Internet
Author: User

Jump and redirect

There are times when a client request needs to be forwarded to another servlet or even another server after it arrives at the service side , which requires jumps and redirects.

Difference

In general, jumps are intra-server jumps, such as forwarding requests from one servlet to another servlet processing, internally. The redirect is to tell the client to go elsewhere and request resources. For example, someone needs to go to the Personnel department to print proof of income, and the income certificate needs to be stamped by the Finance department. Jump Processing method is: personnel door people tell you, you wait, I go to the income certificate print out, then I go to the Finance department stamp, all the information ready, I will give you all. The redirection is handled by the Personnel department to print out the revenue stream and tell you that their work has been done, and now you need to find your own stamp of finance.

This is the essential difference between jump and redirect, when the server jumps, the client address bar does not change, while the client redirects, the address bar changes.

Jump Example

Jumps need to use the Requestdispacher object, which can be obtained from HttpRequest or ServletContext.

The following is a call to the Getrequestdispacher () method from the HttpRequest object to get the Requestdispacher and jump to the sample code:

protected void DoPost (HttpServletRequest req, HttpServletResponse resp)

{

RequestDispatcher Rd = null;

String region = Req.getparameter ("region");

System. out. println (' Region: ' + region);

Switch (region)

{

Case "Asia":

{

System. out. println ("asia~~~");

RD = Req.getrequestdispatcher ("/asiahandler.jsp");

break;

}

Case "EMEA":

{

System. out. println ("emea~~~");

RD = Req.getrequestdispatcher ("/emeahandler.jsp");

break;

}

Case "AMS":

{

System. out. println ("ams~~~");

RD = Req.getrequestdispatcher ("/amshandler.jsp");

break;

}

default:

{

System. out. println ("default~~~");

RD = Req.getrequestdispatcher ("/html/error.html");

break;

}

}

Try

{

Rd.forward (req, resp);

} Catch (servletexception | IOException e)

{

TODO auto-generated Catch block

E.printstacktrace ();

}

}

The following is a call to the Getnameddispacher () method from the ServletContext object to get the Requestdispacher object and jump to the sample code:

protected void DoPost (HttpServletRequest req, HttpServletResponse resp)

{

RequestDispatcher Rd = null;

ServletContext ctx = this. Getservletcontext ();

String region = Req.getparameter ("region");

System. out. println (' Region: ' + region);

Switch (region)

{

Case "Asia":

{

RD = Ctx.getnameddispatcher ("Asiahandler");

break;

}

Case "EMEA":

{

RD = Ctx.getnameddispatcher ("Emeahandler");

break;

}

Case "AMS":

{

RD = Ctx.getnameddispatcher ("Amshandler");

break;

}

}

Try

{

Rd.forward (req, resp);

} Catch (servletexception | IOException e)

{

TODO auto-generated Catch block

E.printstacktrace ();

}

}

The following demonstrates the use of the ServletContext object to call the Getrequestdispacher () method to get the Requestdispacher object, and then implement the jump:

protected void DoPost (HttpServletRequest req, HttpServletResponse resp)

{

RequestDispatcher Rd = null;

ServletContext ctx = this. Getservletcontext ();

String region = Req.getparameter ("region");

System. out. println (' Region: ' + region);

Switch (region)

{

Case "Asia":

{

RD = Req.getrequestdispatcher ("/asiahandler.jsp");

break;

}

Case "EMEA":

{

RD = Req.getrequestdispatcher ("/emeahandler.jsp");

break;

}

Case "AMS":

{

RD = Req.getrequestdispatcher ("/amshandler.jsp");

break;

}

}

Try

{

Rd.forward (req, resp);

} Catch (servletexception | IOException e)

{

TODO auto-generated Catch block

E.printstacktrace ();

}

}

Request redirection is the server sends a new address to the browser, allowing the browser to request a new address. Redirection requires the use of the HttpResponse object to call the Sendredirect () method.

redirect example:

protected void DoPost (HttpServletRequest req, HttpServletResponse resp)

{

Try

{

Resp.sendredirect ("http://www.bing.com");

} Catch (ioexception e)

{

TODO auto-generated Catch block

E.printstacktrace ();

}

}

Request forwarding and redirection in a servlet

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.