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

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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序