未知数据

从头看Java

   ::  :: 联系 :: 聚合  :: 管理
  28 Posts :: 0 Stories :: 10 Comments :: 0 Trackbacks

    Servlets Filter 是Servlet 2.3 规范中新增加的,它是截取用户从客户端提交的请求,在还没有到达需要访问的资源时运行的一个类。它操纵来自客户端的请求,在资源还没有初发送到客户端前截取响应,并处理这些还没有发送到客户端的响应。
Filters 有许多的应用场合。Servlet 2.3 规范建议如下的地方可以应用Filter:
authentication filters
logging and auditing filters
image conversion filters
data compression filters
encryption filters
tokenizing filters
filters that trigger resource access events
XSL/T filters that transform XML content
MIME-type chain filters


package jasso.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class SetCharacterEncodingFilter implements Filter {
    private String encoding;

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
       FilterChain chain) throws IOException, ServletException {
       request.setCharacterEncoding(encoding);
       chain.doFilter(request, response);
    }

    public void init(FilterConfig config) throws ServletException {
       encoding = config.getInitParameter("encoding");
    }
}

web.xml部分

......

<filter>
   <filter-name>encodingFilter</filter-name>
   <filter-class>my.web.filter.SetCharacterEncodingFilter</filter-class>
     <init-param>
       <param-name>encoding</param-name>
       <param-value>Shift_JIS</param-value>
     </init-param>
</filter>
<filter-mapping>
   <filter-name>encodingFilter</filter-name>
   <servlet-name>action</servlet-name>
</filter-mapping>
<servlet>
   <servlet-name>action</servlet-name>
   <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
   <init-param>
     <param-name>config</param-name>
     <param-value>/WEB-INF/config/struts-config.xml,/WEB-INF/config/struts-config-contents.xml</param-value>
   </init-param>
   <init-param>
     <param-name>debug</param-name>
     <param-value>2</param-value>
   </init-param>
   <init-param>
     <param-name>detail</param-name>
     <param-value>2</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
</servlet>

----------------------------------------------

javax.servlet.filter的用法

如何实现一个过滤器呢?
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);
   }
   else
   {
    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("参数名称");
}

posted on 2009-01-07 08:41 wangjc 阅读(701) 评论(0)  编辑  收藏 所属分类: workspace

只有注册用户登录后才能发表评论。


网站导航: