求解答
老师,代码改到完成之后,可以直接访问success.jsp页面了,跟最先的出于安全考虑,直接访问这个页面会跳转到Login.jsp界面想违背,这是怎么回事啊???都不能过滤了,求解答!!!
老师,代码改到完成之后,可以直接访问success.jsp页面了,跟最先的出于安全考虑,直接访问这个页面会跳转到Login.jsp界面想违背,这是怎么回事啊???都不能过滤了,求解答!!!
2014-11-17
<?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;
}
}
举报