| < 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.
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:
Create a class that extends the org.apache.struts.action.RequestProcessor class.
Add a default empty constructor to the RequestProcessor implementation.
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.
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; } }
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.
To deploy and configure our ch04.WroxPlugin, you must:
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.
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.
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.
<?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>
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 > |
|