登录过滤器的问题,输入分类管理和图书管理的地址可以直接访问,新建分量或者图书点提交就返回了登录界面
package com.imooc.filter;
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 com.imooc.user.User;
public class JspFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
String user=(String)req.getSession().getAttribute("ex");
String uri=req.getRequestURI();
String[] urls= {"/KaptchaServlet","/LoginServlet",".js",".css",".ico",".jpg",".png"};
boolean flag=false;
for(String str:urls) {
if(uri.indexOf(str)!=-1) {
flag=true;
break;
}
}
if(flag||user!=null) {
chain.doFilter(request, response);
return;
}else {
res.sendRedirect(req.getContextPath()+"/login.jsp");
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
<filter>
<filter-name>JspFilter</filter-name>
<filter-class>com.imooc.filter.JspFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>JspFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星