这是我Filter的代码
public class FilterServlet extends HttpServlet implements Filter {
private FilterConfig config;
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
this.config=config;
System.out.print("过滤器已执行");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
//获取Filter的配置参数
String loginpage= config.getInitParameter("loginpage");
HttpServletRequest req = (HttpServletRequest) request;
String requestPath = req.getServletPath();
//StringBuffer fileURL = req.getRequestURL();
// 存储上下文路径
request.setAttribute("path", req.getContextPath());
if(!requestPath.endsWith(loginpage)){
// 跳转到登录页面
request.getRequestDispatcher(loginpage)
.forward(request, response);
}else{
chain.doFilter(request, response);
}
}
/**
* Constructor of the object.
*/
public FilterServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
this.config = null;
}
public void init() throws ServletException {
// TODO Auto-generated method stub
}
}
下面是我再web.xml文件中配置的Filter
pathfilter
com.personnel.filter.FilterServlet
loginpage
/login.jsp
pathfilter
<!-- struts2匹配根路径下的全部请求,拦截所以用户请求 ,在日志中简要记录请求的信息-->
/*
我只要把if的内容注释掉就能正常跳转,但是过滤功能无法实现,如上代码的过滤功能能够实现,无法点开除login.jsp以外的界面 ,但是按登录按钮无法进行正常的跳转,一直重定向为登录界面,求大神解答