"Head first Servlets and JSP" Note 10: Request Dispatch (RequestDispatcher)

Source: Internet
Author: User

Let other components take over all requests

; Partial takeover request

Some questions--

The Intercept point is useful.

1. Let other components take over all requests.

 PackageCom.example.web;ImportCom.example.model.BeerExpert;Importjavax.servlet.*;Importjavax.servlet.http.*;ImportJava.io.*;ImportJava.util.*; Public classBeerselectextendsHttpServlet {@Overrideprotected voidDoPost (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {String C= Req.getparameter ("Color"); Beerexpert be=NewBeerexpert (); List result=Be.getbrands (c); Req.setattribute ( "Styles", result); Adds an attribute to the request for use by the JSP. RequestDispatcher view = Req.getrequestdispatcher ("result.jsp"); Instantiate a request dispatcher for JSP View.forward (req, resp); Use the request dispatcher to require the container to prepare the JSP and send requests and responses to the JSP    }}

Why not SendRequest ("result.jsp"), but RequestDispatcher?

2, RequestDispatcher secret.

Where do I get the requestdispatcher? --getrequestdispatcher

    1. ServletRequest, you can use relative paths
    2. ServletContext, the absolute path must be used, i.e. the path must begin with a "/".

How do I use RequestDispatcher?

    1. Call Forward (). It is simple to pass in the request and response as parameters.
    2. It is not necessary to be aware that it is illegal to forward () after responding to a commit (for example, Os.flush).

3. Attribute Scope

"Head first Servlets and JSP" Note 10: Request Dispatch (RequestDispatcher)

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.