session失效后跳转到登陆页面
2024-10-12 09:04:51
一、编写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>
最新文章
- Oracle(创建index)
- apache配置Allow详解及25个常见问题
- .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点
- unity, polygon collider 2D 添加顶点
- 编译boost (windows msvc14)
- COJ 0200 Fibonacci
- 新浪授权认证(不用SDK)
- Java常用类之【Math类、Random类、System类、Runtime类】
- ascii codec can&#39;t decode byte 0xe8 in position 0:ordinal not in range(128) python代码报错
- JSTL遇到的问题
- Docker(六):Docker 三剑客之 Docker Swarm
- Android Studio安装插件Genymotion
- 通过百度地图API--获取全国地图的经纬度
- js 实现异步上传图片+预览
- JavaScript数组方法--filter、find、findIndex
- linux下mysql 配置
- Updating and Publishing a NuGet Package - Plus making NuGet packages smarter and avoiding source edits with WebActivator
- php之sphinx
- myeclipse2014配置多个同版本的Tomcat
- linux中yum命令的解析
热门文章
- 通过Nginx实现负载均衡
- Winform基础
- bootstrap学习笔记(6)
- 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop
- Oracle不足与MySQL优势
- uml入门之14图与图之间的关系
- Javascript的一种代码结构方式——插件式
- 精简CSS代码
- ABAP中Conversion Routine示例
- iOS NSNumber转化NSString之description