jackyrong

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

最近开始学习structs 和hibernate,陆续将心得放上来。structs 用的版本是1。2
   首先,留意到的是org.apache.structs.action.RequestProcessor类,其中真正包含了控制器在处理servlet请求时的控制逻辑。ActionServlet就是通过调用RequestProcessor对象的process()方法,来请求客户端请求。
   其中的proessPreprocess()方法,默认不执行任何操作,开发人员可以在这里进行一些相关的操作,并且扩展RequestProcessor类。比如,可以拒绝非法IP请求,这时重写proessPreprocess()方法,如下:
package kuozhan;
 import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.RequestProcessor;

import org.apache.struts.config.ForwardConfig;
import org.apache.commons.logging.Log;


public class MyRequestProcessor extends RequestProcessor {
 
 public MyRequestProcessor() {}
 
protected boolean processPreprocess( HttpServletRequest request,
                                     HttpServletResponse response ){
 
  boolean continueProcessing = true;
 
  // Get the name of the remote host and log it
  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."))){
     // Not the localhost, so don't allow the host to access the site
     continueProcessing = false;
 
     try{
       response.sendRedirect( error.jsp");
     }catch( Exception ex ){
       log.error("Problem sending redirect from processPreprocess()") ;
     }
  }
  return continueProcessing;
}

}

  上面将凡是不是从本地计算机发出的请求,看作错误处理,并记录在日志上。
 注意扩展requestprocessor类时,最后要在struct-config.xml中加入<controller>元素,如下
 <controller processorClass="kuozhan.example"/>

posted on 2005-09-06 21:06  jackyrong的世界  阅读(1061)  评论(0编辑  收藏  举报