启东市格莱特石化设备厂
主营产品:喷射器  混合器  过滤器  可移动式轻型搅拌机  

关于JSP过滤器的问题

      关于JSP过滤器的问题package com.project.filter; importjavax.servlet.FilterChain; import javax.servlet.ServletRequest;import javax.servlet.ServletResponse; import java.io.IOException;import javax.servlet.Filter; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importjavax.servlet.ServletException; import javax.servlet.FilterConfig;import javax.servlet.http.HttpSession; public class SignonFilterimplements Filter { String LOGIN_PAGE="index.jsp"; protectedFilterConfig filterConfig; //
过滤处理的方法 public void doFilter(final ServletRequest req,finalServletResponse res,FilterChain chain)throwsIOException,ServletException { HttpServletRequest hreq =(HttpServletRequest)req; HttpServletResponse hres =(HttpServletResponse)res; HttpSession session = hreq.getSession();boolean isLog=false; try {isLog=((String)session.getAttribute("isLog")).equals("1");System.out.println(isLog); if(isLog) {System.out.println("在SignonFilter中验证通过"); //
验证成功,继续处理 chain.doFilter(req,res); } else {System.out.println("被SignonFilter拦截一个未认证的请求"); //
验证不成功,让用户登录。 hres.sendRedirect(LOGIN_PAGE); //req.getRequestDispatcher("index.jsp").forward(req,res); //requestDispatcher.forward(request.response); } } catch(Exception e){ e.printStackTrace(); } } public void setFilterConfig(finalFilterConfig filterConfig) { this.filterConfig=filterConfig; }//销毁过滤器 public void destroy() { this.filterConfig=null; } /***初始化过滤器,和一般的Servlet一样,它也可以获得初始参数。 */ public void init(FilterConfigconfig) throws ServletException { this.filterConfig = config; } }在我的这个过滤器中,session.getAttribute("isLog")中的"isLog"已经在LoginAction中用session.setAttribute("isLog",newString("0"))设为假,也就是说未通过验证,但那个判断参数isLog却一直是true.另外就是如果我把if(isLog)改为if(false)下面的hres.sendRedirect(LOGIN_PAGE);转到登陆页面不成功,在eclips中提示空指针错误session.setAttribute("isLog",new String("false")) ORsession.setAttribute("isLog",new String("true")) java 中不象c那样的,if(0)表示假,if(1)表示真.而是只用true 和false来表示真假的,你的判断条件有点问题.看看是不是这个问题.
产品搜索
联系方式
产品目录
Copyright@ 2003-2012  启东市格莱特石化设备厂版权所有      苏ICP备09053172号-1   网站管理入口
网络推广:化工设备网  技术支持: