首页 > 学院 > 开发设计 > 正文

开发框架-Struts里过滤器的简单使用

2019-11-17 05:51:52
字体:
来源:转载
供稿:网友

网上过滤器的使用例子.代码比比皆是.

我这也是一个非常简单.好用的方法.

可供初学者很好的使用!

步骤只要两个:

一是你只要在你的WEB根目录下打开web.xml,然后在父节点加上Filter节点就行.

代码如下:

    Set Character Encoding
    com.yirong.framework.filters.SetCharacterEncodingFilter
   
      encoding
      GBK
   
   
      ignore
      true
   
 
 
    Set Character Encoding
    /*
 

二是:把过滤器的代码(如下代码)放在你的工程目录的源代码管理目录中.
比如我这边的目录是:/myweb/src/Beans
(Beans是我自建的包名)

package Beans;

import org.apache.struts.action.RequestPRocessor;
import javax.servlet.*;
import javax.servlet.http.*;

public class MyRequestProcessor extends RequestProcessor {
    public MyRequestProcessor() {
    }

    protected boolean processPreprocess(HttpServletRequest request,HttpServletResponse response){
        boolean cp=true;
        String remoteHost=request.getRemoteHost();
        if(remoteHost==null || !remoteHost.startsWith("126.")){
            cp=false;
            try{
                response.sendRedirect("iperror.jsp");
            }
            catch(Exception ex){
                ex.printStackTrace();
            }
        }
        System.out.println("--test--");
        return cp;
    }
}

这样一切都可搞定了.
每次有请求它都有将通过过滤器...
简单又实用.!!


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表