最近研究如果用原生的filter来判别session存在否或者过期否。来跳转到的页面实例,下载来展示代码。
因为顾虑器是每次请求能会进入的,所以可以设置了,进行拦截判断
1.配置web.xml
<filter> <filter-name>backendfilter</filter-name> <filter-class>com.sun.backfilter</filter-class> </filter> <filter-mapping> <filter-name>backfilter</filter-name> <url-pattern>/backend/issues/*</url-pattern> <url-pattern>/backend/repairmen/*</url-pattern> <url-pattern>/backend/payment/*</url-pattern> </filter-mapping>
2.新建backfilter类,实现filter接口。
package com.xyt.backend; import java.io.ioexception; import javax.servlet.filter; import javax.servlet.filterchain; import javax.servlet.filterconfig; import javax.servlet.servletexception; import javax.servlet.servletrequest; import javax.servlet.servletresponse; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; import com.xyt.common.logging; import com.xyt.common.redisclient; import com.xyt.jdbc.entity.admin; /** * 后台管理系统登录拦截器 */ public class backfilter implements filter { @override public void init(filterconfig filterconfig) throws servletexception { //useless } @override public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception { httpsession sess = ((httpservletrequest)request).getsession(); string ad = sess.getattribute("admin"); if(ad==null ){ ((httpservletresponse)response).sendredirect(((httpservletrequest)request).getcontextpath()+"//login"); }else{ chain.dofilter(request, response); } } @override public void destroy() { //useless } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。