【转】如何实现一个过滤器

1.所在的类实现Filter接口
 public interface Filter
public void init(FilterConfig filterConfig)throws ServletException
过滤器初始化是在容器启动时自动初始化
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletExcepton
在标准Servlet中的doGet或doPost 方法中,传递参数为:HttpServletRequest、HttpServletResponse
过滤器销毁
public void destroy()

package cn.mldn.lxh.filter
import java.io.*;
import javax.servlet.*;
public class FirstFilter implements Filter
{
  public void init(FilterConfig config)throws ServletException
  {
  System.out.println("过滤器初始化");
  }
  public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException
{
System.out.println(过滤doFilter);
}
  public void destroy()
{
System.out.println(过滤器销毁);
}
}

web.xml文件配置
<filter>
<filter-name>first</filter-name>
<filter-class>cn.mldn.lxh.filter.FirstFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>first</filter-name>

与servlet中的url-pattern不一样,此处表示对那个页面进行过滤,如果全部都过滤,则"/*"
<url-pattern>/*</url-pattern>
</filter-mapping>

如果过滤器要将内容传递到目的地,则需要FilterChain,将请求继续向下转发

过滤器也会执行两次:FilterChain这前执行一次,之后再执行一次

1.过滤非法文字:
package cn.mldn.lxh.filter;
import java.io.*;
import javax.servlet.*;
public class CharFilter implements Filter
{
  public void init(FilterConfig filterConfig)throws ServletException
  {
  //System.out.println("init");
  }
  public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException
{
String content=request.getParameter("content");
//如果indexOf返回-1则表示没有查到所要的内容
if(content!=null)
if(content.indexOf("AAA")==-1)
{
chain.doFilter(request,response);
}
esle
{
System.out.println("有非法文字");
//如果需要的话,此处依然可以使用RequestDispatcher进行跳转
}
}
else
{
chain.doFilter(request,response);
}
}
public void destroy()
{
//System.out.println("过滤器销毁");
}

<filter>
<filter-name>char</filter-name>
<filter-class>cn.mldn.lxh.filter.CharFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>char</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2.对内容进行统一的编码
setCharacterEncoding("GB2312");

package cn.mldn.lxh.filter;
import java.io.*;
import javax.servlet.*;
public class EncodingFilter implements Filter
{
public void init(FilterConfig filterConfig)throws ServletException
{
//System.out.println("过滤器初始化");
}
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException
{
try{
request.setCharacterEncoding("GB2312");
}
catch(Exception e)
{}
chain.doFilter(request,response);
}
public void destroy()
{
//System.out.println("销毁");
}
}

<filter>
<filter-name>encoding</filter-name>
<filter-class></filter-class>
</filter>

<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

3.登陆验证
package cn.mldn.lxh.filter;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginFilter implements Filter
{
public void init(FilterCoinfig filterConfig)throws ServletException
{
//System.out.println("过滤器初始化");
}
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException
{
//Session属于http范畴,把以ServletRequest对象需要先转换成HttpServletRequest对象
HttpServletRequest req=(HttpServletRequest)request;
HttpSession session =req.getSession();
//如果session不为空,则可以浏览其它页面
if(session.getAttribute("uname")!=null)
{
chain.doFilter(request,response);
}
else
{
//通过requestDispatcher跳转到登陆面
request.getRequestDispatcher("login.jsp").forward(request,response);
}
}
public void destroy()
{
//System,out.println("销毁");
}
}

<filter>
<filter-name>login</filter-name>
<filter-class>cn.mldn.lxh.filter.LoginFilter</filter-class>
<init-param>
  <param-name>参数名称</param-name>
  <param-value>参数值</param-value>
  </init-param>
</filter>
<filter-mapping>
<filter-name>login</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

读取web.xml中的参数的方法:

public void init(FilterConfig arg0) throws ServletException {
String 参数值= arg0.getInitParameter("参数名称");
}


转自:http://topic.csdn.net/u/20071221/15/53b6f2bd-51a3-47bb-8331-0c39e99f52fd.html

你可能感兴趣的:(过滤器)