一、编写Filter拦截器类 

 package com.pv.utils;

 import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; public class RequestFilter implements Filter {
private FilterConfig config;
//实现初始化方法
public void init(FilterConfig config){
this.config = config;
}
public void destroy(){
this.config = null;
} @Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
req.setCharacterEncoding("utf-8");
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession();
//获取客户请求的页面
String url = request.getServletPath();
System.out.println("wangming"+url);
if((!request.getServletPath().equals("/login.jsp"))
&&(!request.getServletPath().equals("/solar.jsp"))
&&(!request.getServletPath().equals("/index.jsp"))){
System.out.println(request.getServletPath());
if(session.getAttribute("username")==null){
session.invalidate();
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
out.println("<script language='javascript' type='text/javascript'>");
out.println("alert('由于你长时间没有操作,导致Session失效!请你重新登录!');top.location.href='" + request.getContextPath() + "/solar.jsp'");
out.println("</script>");
}else{
chain.doFilter(request , response );
}
}else{
chain.doFilter(request , response );
}
}
}

二、在web.xml中配置拦截器  

 <session-config>
<session-timeout>1</session-timeout>
</session-config> <filter>
<filter-name>RequestFilter</filter-name>
<filter-class>com.pv.utils.RequestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>RequestFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

最新文章

  1. Oracle(创建index)
  2. apache配置Allow详解及25个常见问题
  3. .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点
  4. unity, polygon collider 2D 添加顶点
  5. 编译boost (windows msvc14)
  6. COJ 0200 Fibonacci
  7. 新浪授权认证(不用SDK)
  8. Java常用类之【Math类、Random类、System类、Runtime类】
  9. ascii codec can&#39;t decode byte 0xe8 in position 0:ordinal not in range(128) python代码报错
  10. JSTL遇到的问题
  11. Docker(六):Docker 三剑客之 Docker Swarm
  12. Android Studio安装插件Genymotion
  13. 通过百度地图API--获取全国地图的经纬度
  14. js 实现异步上传图片+预览
  15. JavaScript数组方法--filter、find、findIndex
  16. linux下mysql 配置
  17. Updating and Publishing a NuGet Package - Plus making NuGet packages smarter and avoiding source edits with WebActivator
  18. php之sphinx
  19. myeclipse2014配置多个同版本的Tomcat
  20. linux中yum命令的解析

热门文章

  1. 通过Nginx实现负载均衡
  2. Winform基础
  3. bootstrap学习笔记(6)
  4. 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop
  5. Oracle不足与MySQL优势
  6. uml入门之14图与图之间的关系
  7. Javascript的一种代码结构方式——插件式
  8. 精简CSS代码
  9. ABAP中Conversion Routine示例
  10. iOS NSNumber转化NSString之description