Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2566538
  • 博文数量: 709
  • 博客积分: 12251
  • 博客等级: 上将
  • 技术积分: 7905
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-17 00:00
个人简介

实现有价值的IT服务

文章存档

2012年(7)

2011年(147)

2009年(3)

2008年(5)

2007年(74)

2006年(431)

2005年(42)

分类: Java

2006-10-12 21:56:27

Filter过滤器的使用
本篇文章是介绍javax.servlet包中Filter的几个功能,一是字符编码过滤,二是拦截指文件夹中的文件,实现登录验证.实现以上两个功能需以下几个步骤:
1、实现javax.servlet.Filter接口,编写两个类,如下:
/**
 * 作用:过滤编码
 */
package filter;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
public class EncodingFilter implements Filter
{
    protected FilterConfig filterConfig;
    private String targetEncoding;
    public EncodingFilter()
    {
        targetEncoding = "gb2312";//直接初始化0912
    }
    public void init(FilterConfig filterconfig) throws ServletException
    {
        filterConfig = filterconfig;
        //targetEncoding = filterconfig.getInitParameter("encoding");//web.xml挂参初始化
    }
    public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain)
        throws IOException, ServletException
    {
        HttpServletRequest httpservletrequest = (HttpServletRequest)servletrequest;
        httpservletrequest.setCharacterEncoding(targetEncoding);
        System.out.println("使用" targetEncoding "对请求进行编码过滤");
        filterchain.doFilter(servletrequest, servletresponse);
    }
    public void setFilterConfig(FilterConfig filterconfig)
    {
        filterConfig = filterconfig;
    }
    public void destroy()
    {
        filterConfig = null;
    }
}
/**
 * 作用:过滤外界非法的进入页面
 * 注意:使用该功能时,一定要把登录页与被过滤文件夹设为同级,否则会出现循环
 */
package filter;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class ManageFilter implements Filter
{
    String LOGIN_PAGE;
    protected FilterConfig filterConfig;
    public ManageFilter()
    {
        LOGIN_PAGE = "/login.jsp";
    }
    public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain)
        throws IOException, ServletException
    {
        HttpServletRequest request = (HttpServletRequest) servletrequest;
        HttpServletResponse response = (HttpServletResponse) servletresponse;
        HttpSession session = request.getSession();
        String key = "";
        try
        {
            //通过验证时在session中存的一个标志
     key = (String) session.getAttribute("key");
          
           
            if (key!= null&&key.equals("ok"))
            {
              
                filterchain.doFilter(servletrequest, servletresponse);
            }
            else
            {
                response.sendRedirect(LOGIN_PAGE);
                System.out.println("被拦截一个未认证的请求");
            }
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
    }
    public void setFilterConfig(FilterConfig filterconfig)
    {
        filterConfig = filterconfig;
    }
    public void destroy()
    {
        filterConfig = null;
    }
    public void init(FilterConfig filterconfig)
        throws ServletException
    {
        filterConfig = filterconfig;
    }
}
2、在web.xml中做一些设置
 
     code
     filter.EncodingFilter
 

 
 
     manage
     filter.ManageFilter
 

 
     code
     /*
 

 
 
     manage
    
     /servlets-examples/*
 

阅读(5874) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~