10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Accessing theWeb Context<br />

Transferring Control to AnotherWeb Component<br />

In some applications, you might want to have one web component do preliminary processing of<br />

a request and have another component generate the response. For example, you might want to<br />

partially process a request and then transfer to another component depending on the nature of<br />

the request.<br />

To transfer control to another web component, you invoke the forward method of a<br />

RequestDispatcher. When a request is forwarded, the request URL is set to the path of the<br />

forwarded page. <strong>The</strong> original URI and its constituent parts are saved as request attributes<br />

javax.servlet.forward.[request-uri|context-path|servlet-path|path-info|query-string].<br />

<strong>The</strong><br />

tut-install/javaeetutorial5/examples/web/bookstore2/src/java/com/sun/bookstore2/dispatcher/Di<br />

servlet, used by a version of the Duke’s Bookstore application described in “<strong>The</strong> Example JSP<br />

Pages” on page 224, saves the path information from the original URL, retrieves a<br />

RequestDispatcher from the request, and then forwards to the JSP page,<br />

tut-install/javaeetutorial5/examples/web/bookstore3/web/template/template.jsp.<br />

public class Dispatcher extends HttpServlet {<br />

public void doGet(HttpServletRequest request,<br />

HttpServletResponse response) {<br />

RequestDispatcher dispatcher = request.<br />

getRequestDispatcher("/template.jsp");<br />

if (dispatcher != null)<br />

dispatcher.forward(request, response);<br />

}<br />

public void doPost(HttpServletRequest request,<br />

...<br />

}<br />

<strong>The</strong> forward method should be used to give another resource responsibility for replying to the<br />

user. If you have already accessed a ServletOutputStream or PrintWriter object within the<br />

servlet, you cannot use this method; doing so throws an IllegalStateException.<br />

Accessing theWeb Context<br />

124<br />

<strong>The</strong> context in which web components execute is an object that implements the<br />

ServletContext interface. You retrieve the web context using the getServletContext<br />

method. <strong>The</strong> web context provides methods for accessing:<br />

■ Initialization parameters<br />

■ Resources associated with the web context<br />

■ Object-valued attributes<br />

■ Logging capabilities<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!