首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

判断session是不是存在的过滤器

2012-11-13 
判断session是否存在的过滤器在web.xml中有如下配置:filterfilter-namemyFilter/filter-namefilter

判断session是否存在的过滤器
在web.xml中有如下配置:

<filter><filter-name>myFilter</filter-name><filter-class>com.test.MyFilter</filter-class></filter><filter-mapping><filter-name>myFilter</filter-name><url-pattern>*.jsp</url-pattern></filter-mapping>

凡是访问以jsp为后缀的页面都会经过过滤器com.test.MyFilter,代码如下:
public class MyFilterimplements Filter {protected FilterConfig filterConfig;public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {HttpServletRequest hreq = (HttpServletRequest) req;HttpServletResponse hres = (HttpServletResponse) res;String requestUrl = hreq.getRequestURI();if (requestUrl.indexOf("index.jsp") > -1) {chain.doFilter(req, res);} else {if (null == hreq.getSession().getAttribute("session_user"))hres.sendRedirect(hreq.getContextPath()+ "/"+"index.jsp");elsechain.doFilter(req, res);}}public void init(FilterConfig filterConfig) throws ServletException {this.filterConfig = filterConfig;}public void destroy() {this.filterConfig = null;}public void setFilterConfig(FilterConfig filterConfig) {this.filterConfig = filterConfig;}}

热点排行