1.创建工具包
2.编写拦截器业务逻辑类容(在此为验证登录效果)
@Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest request=(HttpServletRequest)req; HttpSession session=request.getSession();//获取登录时存在session里面的值 String name=(String)session.getAttribute("name"); if(name!=null){ chain.doFilter(req, res);//如果已登录则直接将请求传递给目标资源 }else{ req.getRequestDispatcher("Login.jsp").forward(req, res);//如果未登录,则返回登录界面 } }
3.配置拦截器拦截范围
<filter>
<filter-name>checkLogin</filter-name>//拦截器名称
<filter-class>com.chinasoft.storemanagement.filter.CheckLogin</filter-class>//类所在全路径
</filter>
<filter-mapping>
<filter-name>checkLogin</filter-name>
<url-pattern>/index.jsp</url-pattern>
<url-pattern>/updatePassword.jsp</url-pattern>//选择需要拦截的文件地址
<url-pattern>/ownInformation.mvc</url-pattern>
</filter-mapping>