赞
踩
- package com.myplan.checkloginfilter;
-
- import javax.servlet.*;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import java.io.IOException;
- import java.io.PrintWriter;
-
- /**
- * 用于检测用户是否登陆的过滤器,如果未登录或超时,则重定向到指的登录页面<p>
- */
- public class CheckLoginFilter extends HttpServlet implements Filter {
-
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain filterChain) {
- try {
- HttpServletRequest hsr = (HttpServletRequest) request;
- HttpSession session = hsr.getSession();
- if (hsr.getRequestURI().startsWith(
- hsr.getContextPath() + "/login.jsp")
- || hsr.getRequestURI().startsWith(
- hsr.getContextPath() + "/login.do")
- || hsr.getRequestURI().equals(hsr.getContextPath() + "/")
- ) {
- // 请求登陆页面,所以放过
- System.out.println("放过login");
- filterChain.doFilter(request, response);
- } else {
- if (session.getAttribute("employeeEmail") != null) {
- //这里session存在,那么则登陆,放过session
- // 已登录,放过请求
- System.out.println("test filter chain");
- filterChain.doFilter(request, response);
- } else {
- //session超期后,自然session就是空了,那么则跳转到指定的页面就可以了
- // 未登录,跳转到登录页
-
- PrintWriter out = response.getWriter();
- out.print("<script>window.top.location.href='http://blog.163.com/girl_lihuiyue@126/blog/"
- + hsr.getContextPath() + "';</script>");
- }
- }
- } catch (ServletException sx) {
- } catch (IOException iox) {
- }
- }
-
- public void destroy() {
-
- }
-
- public void init(FilterConfig filterConfig) throws ServletException {
-
- }
- }
使用filter导致跳转到登陆页面的图片不显示。添加红色字体部分,跳过图片的过滤。
web.xml相应的配置
- <filter>
- <filter-name>CheckLoginFilter</filter-name>
- <filter-class>com.myplan.checkloginfilter.CheckLoginFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>CheckLoginFilter</filter-name>
- <url-pattern>*.jsp</url-pattern>
- </filter-mapping>
- <span style="color:#FF0000;">如果使用session的话,配置
- <session-config>
- <!-- 以分钟为单位 -->
- <session-timeout>30</session-timeout>
- </session-config></span>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。