The RequestProcessor

 < Day Day Up > 



The RequestProcessor is the class that you need to override when you want to customize the processing of the ActionServlet. It contains a predefined entry point that is invoked by the Struts Controller with each request. This entry point is the processPreprocess() method.

Creating Your Own RequestProcessor

If you want to add your own specialized processing to the Controller, implement the processPreprocess() method, adding your specific logic and returning true to continue normal processing. If you want to terminate normal processing, return false to tell the Controller that the current request is complete. The following code snippet shows the default processPreprocess() implementation:

 protected boolean processPreprocess(HttpServletRequest request,   HttpServletResponse response) {   return (true); } 

To create your own RequestProcessor, follow these steps:

  1. Create a class that extends the org.apache.struts.action.RequestProcessor class.

  2. Add a default empty constructor to the RequestProcessor implementation.

  3. Implement your processPreprocess() method.

To see how all of this works, take a look at our example RequestProcessor implementation in Listing 4.2.

Listing 4.2: WroxRequestProcessor.java.

start example
 package ch04; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import java.io.IOException; import java.util.Enumeration; import org.apache.struts.action.RequestProcessor; public class WroxRequestProcessor extends RequestProcessor {   public WroxRequestProcessor() {   }   public boolean processPreprocess(HttpServletRequest request,     HttpServletResponse response) {     log("-----------processPreprocess Logging-------------");     log("Request URI = " + request.getRequestURI());     log("Context Path = " + request.getContextPath());     Cookie cookies[] = request.getCookies();     if (cookies != null) {       for (int i = 0; i < cookies.length; i++) {         log("Cookie = " + cookies[i].getName() + " = " +         cookies[i].getValue());       }     }     Enumeration headerNames = request.getHeaderNames();     while (headerNames.hasMoreElements()) {       String headerName =         (String) headerNames.nextElement();       Enumeration headerValues =         request.getHeaders(headerName);       while (headerValues.hasMoreElements()) {         String headerValue =           (String) headerValues.nextElement();         log("Header = " + headerName + " = " + headerValue);       }     }     log("Locale = " + request.getLocale());     log("Method = " + request.getMethod());     log("Path Info = " + request.getPathInfo());     log("Protocol = " + request.getProtocol());     log("Remote Address = " + request.getRemoteAddr());     log("Remote Host = " + request.getRemoteHost());     log("Remote User = " + request.getRemoteUser());     log("Requested Session Id = "       + request.getRequestedSessionId());     log("Scheme = " + request.getScheme());     log("Server Name = " + request.getServerName());     log("Server Port = " + request.getServerPort());     log("Servlet Path = " + request.getServletPath());     log("Secure = " + request.isSecure());     log("-------------------------------------------------");     return true;   } } 
end example

In the processPreprocess() method we are retrieving the information stored in the request and logging it to the ServletContext log. Once the logging is complete, the processPreprocess() method returns the Boolean value true and normal processing continues. If the processPreprocess() method had returned false, then the Controller would have terminated normal processing and the Action would never have been performed.

Configuring an Extended RequestProcessor

To deploy and configure our ch04.WroxPlugin, you must:

  1. Compile the new RequestProcessor and move it to the Web application's classpath. For our purposes, you should move the compiled class file to the WEB-INF/classes/ch04/ directory of your Web application.

    start sidebar

    Again, for this example we use our application from Chapter 3 as the host of our new Plugin. You can find this complete application in the ch04/src/ directory of this book's source distribution.

    end sidebar

  2. Add a <controller> element to the application's struts-config.xml file. The <controller> is used to describe the new RequestProcessor. An example <controller> entry, describing our new RequestProcessor, is shown in the following code snippet:

     <controller   processor /> 

When you add the <controller> element, it must follow the <action-mappings> element and proceed the <message-resources /> elements in the struts-config.xml file. A full description of the <controller> element and its attributes is included in Chapter 16. If you followed along with the examples in this and the proceeding chapter, then you should have a struts-config.xml file that looks something like Listing 4.3.

Listing 4.3: struts-config.xml.

start example
 <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config>   <form-beans>     <form-bean name="lookupForm"       type="ch04.LookupForm"/>   </form-beans>   <action-mappings>     <action path="/Lookup"       type="ch04.LookupAction"       name="lookupForm" >       <forward name="success" path="/quote.jsp"/>       <forward name="failure" path="/index.jsp"/>     </action>   </action-mappings>   <controller     processor />   <plug-in className="ch04.WROXPlugin"/> </struts-config> 
end example

Restart the Struts Web application.

When this deployment is complete, the new RequestProcessor will take effect. To see the results of these log statements, log in and open the <CATALINA_HOME>/logs/localhost_log.todaysdate.txt file. You will see the logged request at the bottom of the log file.



 < Day Day Up > 



Professional Jakarta Struts
Professional Jakarta Struts (Programmer to Programmer)
ISBN: 0764544373
EAN: 2147483647
Year: 2003
Pages: 183

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