过滤器如何使用 请问: 我有个后台,但是我后台的权限是要先管理员登陆,如果没有的话,过滤器要如何拦截后台的其他管理员操作路径,并且保证没有Bug . 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在Tomcat服务器运行的时候,我前台的.jsp放在一个文件夹里,后台的.jsp放在一个文件夹里,我后台是管理员的操作,如果要操作后台 ,必须要先登陆, 否则过滤器会将你返回到你的登陆界面, 请问这个过滤器要如何操作,才可以实现这样的效果,并且保证没有Bug 谢谢 web.xml:<filter> <filter-name></filter-name> <filter-class></filter-class></filter><filter-mapping> <filter-name></filter-name> <url-pattern></url-pattern><!-- 这里配置你要验证的文件路径 --></filter-mapping> 把登陆后的用户名放入session,如果session没有,则将返回页设置为登陆页 filter class 示例:public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest hreq = (HttpServletRequest) request; HttpServletResponse hres = (HttpServletResponse) response; if (!validate(hreq, hres)) {// 校验没有通过 hreq.getRequestDispatcher(signOnPage).forward(hreq, hres); } else { chain.doFilter(request, response); // -- Process the response -- \\ } } 你那个!validate(hreq,hres) 这个验证方法?? 在这里面用Session来判断,是不是有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() { }} s:iterator 迭代不出数据 jndi连接数据库时怎么获得数据库名和密码 Struts 找不到方法 有谁了解在线购买点卡,并对点卡进行充值的业务.都说说看啊. 我的jsp是用dreamweaver来做的,请问有什么好方法可以调试? 利用jxl生成EXCEL 供客户进行WEB下载产生的疑惑 怎么写这个SQL语句 jsp页面提交乱码 问题很多能给出完整的解决方法给100分 jbuidler9 ,sql ,难题 ?????,高手进来看一下了,在线等 弹出窗口的淡出效果用JS 能实现吗? java 使用浏览器插件
请问这个过滤器要如何操作,才可以实现这样的效果,并且保证没有Bug
谢谢
<filter>
<filter-name></filter-name>
<filter-class></filter-class>
</filter>
<filter-mapping>
<filter-name></filter-name>
<url-pattern></url-pattern><!-- 这里配置你要验证的文件路径 -->
</filter-mapping>
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest hreq = (HttpServletRequest) request;
HttpServletResponse hres = (HttpServletResponse) response; if (!validate(hreq, hres)) {// 校验没有通过
hreq.getRequestDispatcher(signOnPage).forward(hreq, hres);
} else {
chain.doFilter(request, response); // -- Process the response -- \\
}
}
<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() {
}
}