我正在使用两个过滤器,但未应用第二个过滤器,
以下是InvokeServlet.do的代码
request.getRequestDispatcher("/secured/securedResult.jsp").forward(request, response);
和web.xml代码是: -
<servlet-mapping>
<servlet-name>InvokeServlet</servlet-name>
<url-pattern>/InvokeServlet.do</url-pattern>
</servlet-mapping>
<filter>
<filter-name>SecurityFilter</filter-name>
<filter-class>filter.SecurityFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SecurityFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter>
<filter-name>SecurityFilter2</filter-name>
<filter-class>filter.SecurityFilter2</filter-class>
</filter>
<filter-mapping>
<filter-name>SecurityFilter2</filter-name>
<url-pattern>/secured/*</url-pattern>
</filter-mapping>
地址栏上的 http://localhost:8080/OnlineBankingWS/InvokeServlet.do
但是当我点击url http://localhost:8080/OnlineBankingWS/secured/securedResult.jsp
时会调用过滤器(控制台上会打印一些sysout语句)。
SecurityFilter.java
package 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 javax.servlet.http.HttpSession;
public class SecurityFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
HttpSession session = request.getSession();
if (session.getAttribute("userSession") == null) {
String msg = "oops! Your Session Has Been Expired";
request.setAttribute("msg", msg);
request.getRequestDispatcher("loginForm.jsp")
.forward(request, response);
}
arg2.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
SecurityFilter2.java
package 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 javax.servlet.http.HttpSession;
public class SecurityFilter2 implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
String pathInfo=request.getPathInfo();
String contextPath=request.getContextPath();
System.out.println(" pathINfo--> " + pathInfo + " contextPath--> " + contextPath );
arg2.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("in init method of security filter 2");
}
}
上面的过滤器逻辑尚未实现,应该打印语句。
答案 0 :(得分:3)
尝试使用
<filter-mapping>
<filter-name>SecurityFilter2</filter-name>
<url-pattern>/secured/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
请参阅
http://www.javaworld.com/javaworld/jw-01-2006/jw-0102-servlet.html?page=3