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
- ServletRequest, you can use relative paths
- ServletContext, the absolute path must be used, i.e. the path must begin with a "/".
How do I use RequestDispatcher?
- Call Forward (). It is simple to pass in the request and response as parameters.
- 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)