Struts2--针对session使用过滤器
这周带新人在我们系统登录页面做一个单表增删改查功能(如注册),在做的过程中发现跳转的时候存在问题,每次点击新增的按钮后,没有跳转到自己需要的页面,而是跳转到我们系统的登录页面,经过一段时间的琢磨了解到原来是过滤器搞的鬼。
web.xml配置如下:
public class SessionFilter implements Filter {public void init(FilterConfig filterconfig) throws ServletException {}public void destroy() {}public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {HttpServletRequest httprequest = (HttpServletRequest)request;String request_uri = httprequest.getRequestURI();// 对SESSION是否有效进行校验boolean flag = checkSession((HttpServletRequest) request, (HttpServletResponse) response);if (flag)IndivCheck.indivCheck((HttpServletRequest) request);chain.doFilter(request, response);}}private boolean checkSession(HttpServletRequest request,HttpServletResponse response) {// 如果Session失效,返回null;HttpSession session = request.getSession(false);String URI = request.getRequestURI();if ((null != request.getParameter("userCode")&& null != request.getParameter("password"))) {return true;} else {try {String userCode = (String) session.getAttribute(CASFilter.CAS_FILTER_USER);UserMsgResInfo userMsgResInfo = null;UserInfo userInfo = null;String ssoRequester = request.getParameter("ssoRequester");userInfo = (UserInfo) session.getAttribute("userInfo");// 获取页面中的用户,如果为空 返回登录页面if (null == userInfo) {response.sendRedirect("/prpins");return false;}} catch (Exception e) {System.out.println("登录超时,请重新登录");try {response.sendRedirect("/prpins");} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}e.printStackTrace();return false;}return true;}}}