jsp---->过滤器

过滤器

Servlet 程序的主要分类?
· 标准 Servlet(JSP) —— MVC
· 过滤 Servlet(过滤器)
· 监听 Servlet(监听器)

                                                                                                                                                                                        

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

2、如果过滤器要将内容传递到目的地,则需要 FilterChain,将请求继续向下转发
public void doFilter(ServletRequest request,ServletResponse response)throws IOException,ServletException

例子:
1、对用户是否登陆进行过滤(之前是在每个JSP 页面之中判断session)

package cn.filter ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
public class LoginFilter implements Filter
{
public void init(FilterConfig 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.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>login</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
*/

2、网上聊天系统,屏蔽非法文字

package cn.filter ;
import java.io.* ;
import javax.servlet.* ;
public class CharFilter implements Filter
{
public void init(FilterConfig filterConfig)
throws ServletException
{
// System.out.println("** 过滤器初始化...") ;
}
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.filter.CharFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>char</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
*/

3、对请求内容进行统一编码setCharacterEncoding("GB2312") ;

package cn.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>cn.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
*/
 

posted on 2012-08-17 09:58  小强斋太  阅读(175)  评论(0编辑  收藏  举报

导航