写了一个filter,错误就转向首页,但是地址栏仍然显示的是当前页的地址!!

解决方案 »

  1.   

    response.sendRedirec("xxx")
    不能改变地址栏的地址,但仍会转向xxx页面如果你用MVC模式
    //将servlet请求转发给你要转向的页面     
    RequestDispatcher dispather=request.getRequestDispatcher("xxx");
        dispather.forward(request,response);那么 不仅转向xxx页面而且地址栏的地址就会改变
      

  2.   

    public void doFilter(ServletRequest req, ServletResponse res,
    FilterChain arg2) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    HttpSession session = request.getSession();
    String path = request.getServletPath();
    if (!"/index.jsp".equals(path)) {
    }
    String user = (String) session.getAttribute(Constants.USERID);
    if (user == null && !"/index.jsp".equals(path)) {
    String errors = "请重新登录。";
    request.setAttribute("errors", errors);
    response.sendRedirect(request.getSession().getServletContext()
    .getContextPath()
    + "/index.jsp");
    } else {
    arg2.doFilter(request, response);
    }
    }
      

  3.   

    原来直接走JSP呀 ,你转发登陆的SERVLET试试。
    你确定  localhost项目名/index.jsp能显示
      

  4.   

    刚开始就是用的3楼的方法,也一样的效果。
    因为看到sendRedirect相当于location.href,所以认为时可以的。结果还是不行!
      

  5.   


    筷子是用来喝汤,勺子是用来夹面的对吧。楼主,你这段多余的代码用来做什么的if (!"/index.jsp".equals(path)) {
      

  6.   


    显示两种方法都能显示的。首页没有做action映射(struts2),应为不想首页用filter。
      

  7.   

    用servlet的话 别把web.xml中的东西写错了
      

  8.   

     dispather.forward(request,response); 
    我没记错的话,是服务器端跳转,地址栏不变,好久没耍SERVLET 的不知道是对是错
      

  9.   


    刚开始写的判断url来源的条件,属于多余还没有删除!不好意思!
      

  10.   

    使用response.sendRedirect()地址栏将改变 
    使用request.getRequestDispatcher().forward(request,response)地址栏中保持不变 
      

  11.   

    有没有用于iframe或者frame啊?
    如果用了,那么对地址栏是没有影响的!
      

  12.   

    使用response.sendRedirect()地址栏将改变 
    使用request.getRequestDispatcher().forward(request,response)地址栏中保持不变
      

  13.   

    使用response.sendRedirect()地址栏将改变 
    使用request.getRequestDispatcher().forward(request,response)地址栏中保持不变