免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 854 | 回复: 0
打印 上一主题 下一主题

两个Filter类:登录过滤和编码过滤 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-26 00:30 |只看该作者 |倒序浏览
LoginFilter.java:
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.FilterConfig;
/**//*
*
* @author Ace
* web.xml配置如下:
*  
* login
*
* org.waityou.news.util.filters.LoginFilter
*
*  
*  
* login
* /manager/*
*
*/
public class LoginFilter implements Filter {
    protected FilterConfig filterConfig;
    public void doFilter(final ServletRequest req, final ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest hreq = (HttpServletRequest) req;
        HttpServletResponse hres = (HttpServletResponse) res;
        String isLog = (String) hreq.getSession().getAttribute("isLog");
        if ((isLog != null) && ((isLog.equals("true")) || (isLog == "true")))//检查是否登录
        {
            chain.doFilter(req, res);
            return;
        } else
            hres.sendRedirect("/error.html");//如果没有登录,把视图派发到登录页面
    }
    public void destroy() {
        this.filterConfig = null;
    }
    public void init(FilterConfig config) {
        this.filterConfig = config;
    }
    public void setFilterConfig(final FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
    }
}
SetCharacterEncodingFilter.java:
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;
/** *//**
* @author Ace 编码过滤,一律设置成UTF-8
*
* web.xml中配置如下:
*
* Set Character Encoding
*
* org.waityou.news.util.filters.SetCharacterEncodingFilter
*  
*  
*
* Set Character Encoding
* /*
*
*/
public class SetCharacterEncodingFilter implements Filter {
   
    public void destroy() {}
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        // 传递控制到下一个过滤器
        chain.doFilter(request, response);
    }
   
    public void init(FilterConfig filterConfig) throws ServletException {}
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/25374/showart_190550.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP