emily_yq
2014-11-17 11:10
老师,代码改到完成之后,可以直接访问success.jsp页面了,跟最先的出于安全考虑,直接访问这个页面会跳转到Login.jsp界面想违背,这是怎么回事啊???都不能过滤了,求解答!!!
我也遇到这样的问题 但觉得代码没问题
亲,根据你的代码进行测试,代码是没有问题,项目部署之后,访问success.jsp,可直接跳转到Login.jsp.你看再试试
百度一下,你就知道
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>first</servlet-name>
<servlet-class>com.imcoo.first</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>first</servlet-name>
<url-pattern>/servlet/first</url-pattern>
</servlet-mapping>
<filter>
<filter-name>firstfilter</filter-name>
<filter-class>com.imcoo.firstfilter</filter-class>
<init-param>
<param-name>noLoginPath</param-name>
<param-value>Login.jsp;fail.jsp;first</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>firstfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
package com.imcoo;
import java.io.IOException;
import java.util.Enumeration;
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 javax.servlet.http.HttpSession;
public class firstfilter implements Filter {
private FilterConfig config;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest) arg0;
HttpServletResponse response=(HttpServletResponse) arg1;
HttpSession session=request.getSession();
String noLoginPath=config.getInitParameter("noLoginPath");
if(noLoginPath!=null){
String[] array=noLoginPath.split(";");
for(int i=0;i<array.length;i++){
if(array[i]==null||"".equals(array[i])) continue;
if(request.getRequestURI().indexOf(array[i])!=-1){
arg2.doFilter(arg0, arg1);
return;
}
}
}
if(session.getAttribute("name")!=null){
arg2.doFilter(arg0, arg1);
}
else{
response.sendRedirect(request.getContextPath()+"/Login.jsp");
}
System.out.println(session.getAttribute("name"));
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
config=arg0;
}
}
package com.imcoo;
import java.io.IOException;
import java.util.Enumeration;
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 javax.servlet.http.HttpSession;
public class firstfilter implements Filter {
private FilterConfig config;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest) arg0;
HttpServletResponse response=(HttpServletResponse) arg1;
HttpSession session=request.getSession();
String noLoginPath=config.getInitParameter("noLoginPath");
if(noLoginPath!=null){
String[] array=noLoginPath.split(";");
for(int i=0;i<array.length;i++){
if(array[i]==null||"".equals(array[i])) continue;
if(request.getRequestURI().indexOf(array[i])!=-1){
arg2.doFilter(arg0, arg1);
return;
}
}
}
if(session.getAttribute("name")!=null){
arg2.doFilter(arg0, arg1);
}
else{
response.sendRedirect(request.getContextPath()+"/Login.jsp");
}
System.out.println(session.getAttribute("name"));
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
config=arg0;
}
}
亲,把配置文件和过滤器的代码贴出来看看吧
Java Web开发技术应用——过滤器
70492 学习 · 258 问题
相似问题