public class isLoginFilterIO implements Filter { //FilterConfig可用于访问Filter的配置信息
 private FilterConfig config;
public void destroy() {
// TODO Auto-generated method stub
this.config = null;
System.out.println("拦截器结束……");
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//String encoding = config.getInitParameter("encoding");  //还有这里取不到值
//登录页面
/*String loginPage = config.getInitParameter("loginPage");
System.out.println(loginPage);*/
//设置request编码用的字符集
request.setCharacterEncoding("gb2312");                 
HttpServletRequest req = (HttpServletRequest)request;
HttpSession session = req.getSession();
//PrintWriter out = response.getWriter();
//获取客户请求的页面
String login = "MyHtml.html";
String addjsp = "addJsp.jsp";
String requestPath = req.getServletPath();
 System.out.println("请求页面"+requestPath);
//如果session范围的user为null,即表明没有登录
 //且用户请求的既不是登录页面
 if(session.getAttribute("user") == null && !requestPath.endsWith(login) && !requestPath.endsWith(addjsp)){
 //未登录,返回登录页面
 //提示:未登录,请登录后操作
 //request.setAttribute("tip","请先登录");
 //out.print("<script>alert('请先登录!')</script>");
// out.print("<script>window.location.href='Myhtml.html'</script>");
 System.out.println("拦截到未登录用户");
 request.getRequestDispatcher("/MyHtml.html").forward(request, response);
 } else {
 System.out.println(session.getAttribute("user"));
 chain.doFilter(request, response);
 }     
} public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
this.config = config;
System.out.println("拦截器开始……");
}}

解决方案 »

  1.   

    <filter>
    <!-- Filter的名字  -->
    <filter-name>isLoginFilterIO</filter-name>
    <!-- Filter的实现类  -->
    <filter-class>com.oracletest.Filter.isLoginFilterIO</filter-class>
    <!-- 3个INIT-param的3个参数 -->
    <init-param>
     <param-name>encoding</param-name>
     <param-value>GBK</param-value>
     </init-param>
     <init-param>
     <param-name>loginPage</param-name>
     <param-value>/MyHtml.html</param-value>
     </init-param>
    <!--  
    <init-param>
    <param-name>proLogin</param-name>
    <param-value>GBK</param-value>
    </init-param>-->
    </filter>
      

  2.   

    public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub this.config = config; System.out.println("拦截器开始……"); }这个地方this.config = config 应该是this.config = arg0;原因赋值没有正确。所以在dofilter那里的config没有取到值。所以就是空值啦!!