目的是完成未正规登陆的过滤问题
过滤类是这么写的public class OnlineFilter implements javax.servlet.Filter {
String loginPage="index.jsp"; //没有登录的话,你要跳转的目录
protected FilterConfig filterConfig;
public void destroy() {
filterConfig=null;
} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession session = req.getSession();
String isLogin = (String)session.getAttribute("login");
System.out.println(isLogin); //测试
if (isLogin!=null && isLogin.equals("yes")){ //判断session值是否正确
chain.doFilter(request,response);
}else{
res.sendRedirect(loginPage); //失败的话,sendRedirect到指定页面
}
} public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig=filterConfig;
}
}
web.xml这样配置的(片段)<filter>
<filter-name>IsLogin</filter-name>
<filter-class>filter.OnlineFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>IsLogin</filter-name>
<url-pattern>/client/*</url-pattern>
</filter-mapping>
意思想过滤client目录下所有文件
index.jsp是在webroot目录下的如下结构
webroot
| |
| |
| client
| |
| |-----------XXX.jsp
|
index.jsp
若我尝试直接用http:localhose:8888/myproject/client/xxx.jsp 类型登陆时 就一直卡住页面 看控制台打印 null 一直在打印null 死循环
过滤类是这么写的public class OnlineFilter implements javax.servlet.Filter {
String loginPage="index.jsp"; //没有登录的话,你要跳转的目录
protected FilterConfig filterConfig;
public void destroy() {
filterConfig=null;
} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession session = req.getSession();
String isLogin = (String)session.getAttribute("login");
System.out.println(isLogin); //测试
if (isLogin!=null && isLogin.equals("yes")){ //判断session值是否正确
chain.doFilter(request,response);
}else{
res.sendRedirect(loginPage); //失败的话,sendRedirect到指定页面
}
} public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig=filterConfig;
}
}
web.xml这样配置的(片段)<filter>
<filter-name>IsLogin</filter-name>
<filter-class>filter.OnlineFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>IsLogin</filter-name>
<url-pattern>/client/*</url-pattern>
</filter-mapping>
意思想过滤client目录下所有文件
index.jsp是在webroot目录下的如下结构
webroot
| |
| |
| client
| |
| |-----------XXX.jsp
|
index.jsp
若我尝试直接用http:localhose:8888/myproject/client/xxx.jsp 类型登陆时 就一直卡住页面 看控制台打印 null 一直在打印null 死循环
解决方案 »
- 一个swing问题,请高手指点。
- 关于基数排序 eclipse总是报出界
- Mysql中blob相关的问题,请大家帮忙解决一下!急!!!
- 请教,JDBC能否调用SQL SERVER中的自定义函数?
- 苦求,一個關於SQL的問題!
- 初学JAVA,什么教材上手比较好?
- 小问题,请拉我一把
- 高手帮我看看下面的程序哪里出了问题!高分!
- 中秋快乐!内存溢出.无法解决.放段代码大家共享.
- java遇到一个奇怪的问题,望大侠解答~
- UnitTest如果根据method的return 结果就比较容易测,如果是void的method,可能实现的业务功能比较重要,但是并没有返回或者返回值并不重要
- JTextPane插入图片的问题
chain.doFilter(request,response);
return;
}else{
res.sendRedirect(loginPage); //失败的话,sendRedirect到指定页面
}
比如在webroot下的另外个文件夹名字为 port
如果把这个也加上去了?
根本就没有达到你要的结果
根本看不到关于你是怎么过滤client目录下所有文件
比如我们教程中的例子
loginPage就在/login.jsp上。而受保护的对象在/admin/*,这样就不会出现死循环。我们教程中的例子:
http://www.family168.com/tutorial/jsp/html/jsp-ch-07.htm#jsp-ch-07-02或者考虑使用forward
<filter-name>IsLogin</filter-name>
<url-pattern>/port/*</url-pattern>
</filter-mapping>
我直接用最SB的办法
改成这样
String path = ((HttpServletRequest) request).getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
if (isLogin!=null && isLogin.equals("yes")){ //判断session值是否正确
chain.doFilter(request,response);
return;
}else{
res.sendRedirect(basePath+"index.jsp"); //失败的话,sendRedirect到指定页面
}
}