Recipe6.3.Returning the HTTP Response


Recipe 6.3. Returning the HTTP Response

Problem

You want to create a response in the Action and send that response to the client instead of forwarding to another action or JSP page.

Solution

Use the standard methods provided by the HttpServletResponse object to write the response. Then return null instead of an ActionForward from the execute() method of the Action. Example 6-2 shows an Action that creates and returns a simple response.

Example 6-2. Writing the HTTP response in an action
package com.oreilly.strutsckbk.ch06; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class ResponseWriterAction extends Action {          public ActionForward execute( ActionMapping mapping,                                    ActionForm form,                                   HttpServletRequest request,                                    HttpServletResponse response)             throws Exception {         response.setContentType("text/html");         PrintWriter out = response.getWriter( );         out.write("<html><head></head><body>Hello World!</body></html>");          return null;   }         }

Discussion

The typical Action returns an ActionForward from its execute( ) method. The returned ActionForward is evaluated and processed by the Struts request processor. The returned forward specifies the path to a resource, like a JSP page, that generates the actual HTTP response. However, you can generate the response in the Action. If you do so, you must return null from the Action's execute() method. This tells the Struts request processor no ActionForward will follow; therefore, the response should be returned to the client.

Many scenarios exist in which you may want to write the response. Applications that dynamically generate non-HTML content from binary data can use this approach. These applications can write binary content, such as images and PDF documents, directly to the response and can set the content type in the HTTP header to the appropriate MIME type for handling by the browser.

Though the Action may return the response, the Action should delegate the writing of the response to a custom class not tied to the Struts API.

See Also

The traditional Java mechanism for creating an HTTP response is the servlet. If you are writing the response in your Action, you may want to consider using a servlet to do this instead. Your Action would forward the request to that servlet using an ActionForward. The ForwardAction described in Recipe 6.5 shows a technique for wrapping access to servlets with Struts actions.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net