woshicaiweina 2016-06-11 13:11 采纳率: 100%
浏览 6492
已采纳

Filter过滤器,过滤后登陆界面无法正常跳转,好像一直重定向到登陆界面,就大神看看

这是我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以外的界面 ,但是按登录按钮无法进行正常的跳转,一直重定向为登录界面,求大神解答

  • 写回答

3条回答 默认 最新

  • lzj0327 2016-06-11 14:16
    关注
     通过你的if判断,现在把不是链接到登陆页面的链接全都换成了到登陆页面,要过滤的话通过session拦截未登录的就行了
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程