1. 程式人生 > >javax.servlet.Filter 的過濾器的使用

javax.servlet.Filter 的過濾器的使用

 
javax.servlet.Filter 的應用
 
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
     <!-- <url-pattern>/ShuiBaoRHPWK_NEW/system/ruhepaiwukou/*</url-pattern>-->
     /servlets-examples/