1. 在工程中建类:MyRequestProcessor .java
package classmate;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.RequestProcessor;
import org.apache.commons.logging.Log;
public class MyRequestProcessor extends RequestProcessor {
public MyRequestProcessor(){
}
protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {
boolean continueProcessing = true;
//get userIP
String remoteHost = request.getRemoteHost();
log.info("Request from host: " + remoteHost);
//make sure the host is from one that you expect
if((remoteHost == null) || !remoteHost.startsWith("127.")){
continueProcessing = false;
// forward failpage
try{
response.sendRedirect("error.jsp");
}catch(Exception ex){
log.error("Problem sending redirect form processPreprocess()");
}
}
return continueProcessing;
}
}
2. 在struts-config.xml里如入这一句:<controller processorClass="classmate.MyRequestProcessor"></controller>
这样在用户请求的时候就会根据条件自动检测了。并可以在Tomcat控制台上看到:- Request from host: 127.0.0.1