网上过滤器的使用例子.代码比比皆是.
我这也是一个非常简单.好用的方法.
可供初学者很好的使用!
步骤只要两个:
一是你只要在你的WEB根目录下打开web.xml,然后在父节点加上Filter节点就行.
代码如下:
Set Character Encodingcom.yirong.framework.filters.SetCharacterEncodingFilterencodingGBKignoretrueSet 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;}}
这样一切都可搞定了.
每次有请求它都有将通过过滤器…
简单又实用.!!
却坐在不足一平米的椅子上。