首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

过滤器怎么使用

2012-01-24 
过滤器如何使用请问:我有个后台,但是我后台的权限是要先管理员登陆,如果没有的话,过滤器要如何拦截后台的

过滤器如何使用
请问:
          我有个后台,但是我后台的权限是要先管理员登陆,如果没有的话,过滤器要如何拦截后台的其他管理员操作路径,并且保证没有Bug   .
         


[解决办法]
web.xml
<filter>
<filter-name> adminfilter </filter-name>
<filter-class> xxx.xxx </filter-class> <!--对应后台过滤器类-->
</filter>
<filter-mapping>
<filter-name> adminfilter </filter-name>
<url-pattern> /admin/*.* </url-pattern> <!--admin是存放管理员页面的目录-->
</filter-mapping>

后台过滤器
public class LoginSession implements Filter {

public void init(FilterConfig arg0) throws ServletException {

}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//身份验证,防止盗链
HttpServletRequest myRequest = (HttpServletRequest)request;
HttpSession mySession = myRequest.getSession();
if(mySession.getAttribute( "myUserVO ")==null){
PrintWriter myWriter = response.getWriter();
myWriter.print( " <script> parent.location.href= '/AssetManageG4/login/login.jsp ' </script> ");
}
chain.doFilter(request,response);
}

public void destroy() {

}
}

热点排行