4.RequestProcessor: 有关的ActionServlet的实际处理都是在RequestProcessor类中完成的。我们也可以创建我们自己的RequestProcessor类,这需要继承RequestProcessor类。并且要有一个缺省的空的构造器。在这个自定义的RequestProcessor类中重写相关的方法,一般都是重写processXXX()方法。 ◆扩展RequestProcessor类 扩展Processor类按下面的步骤完成: ① 创建一个继承于org.apache.struts.action.RequestProcessor的类 ② 添加一个缺省的空的构造器 ③ 实现想要重写的方法 例子: package wiley; 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 WileyRequestProcessor extends RequestProcessor { public WileyRequestProcessor() { } 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; } } ◆配置RequestProcessor: 在struts-config.xml文件中配置<controller/>元素。如下: <controller processorClass=”wiley.WileyRequestProcessor” /> 详细配置信息见”struts-config.xml配置文件讲解”。
|