半翻譯半整理的一些struts的東東,很淺顯的,歡迎指正(3)
發(fā)表時間:2023-07-17 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]4.RequestProcessor:有關(guān)的ActionServlet的實際處理都是在RequestProcessor類中完成的。我們也可以創(chuàng)建我們自己的RequestProcessor類,這需要繼...
4.RequestProcessor:
有關(guān)的ActionServlet的實際處理都是在RequestProcessor類中完成的。我們也可以創(chuàng)建我們自己的RequestProcessor類,這需要繼承RequestProcessor類。并且要有一個缺省的空的構(gòu)造器。在這個自定義的RequestProcessor類中重寫相關(guān)的方法,一般都是重寫processXXX()方法。
◆擴展RequestProcessor類
擴展Processor類按下面的步驟完成:
① 創(chuàng)建一個繼承于org.apache.struts.action.RequestProcessor的類
② 添加一個缺省的空的構(gòu)造器
③ 實現(xiàn)想要重寫的方法
例子:
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配置文件講解”。