使用调用RequestDispatcher.forward()双转发的ServletRequest

2019-10-17 05:14发布

我的Java Web应用程序使用两个servlet来控制表格处理和导航/AppControl/ViewControl

AppControl处理表单提交,然后将请求转发给ViewControl,确定哪些页面进行处理,并重新将请求转发到“下一个”页面。

(之间的Servlet)前进1工作好; 但第二(从ViewControl JSP页面)malforms的URL,我得到一个404。

该小服务程序都映射到上下文的根中,JSP文件是在名为子文件夹/视图/

转发#1变为从/AppControl/ViewControl和转发#2变从/ ViewControl到/view/xxx.jsp ,但什么出现在浏览器localhost:8080/view//view/xxx.jsp ,这显然是不它应该已经走了。 注意上下文从发送到浏览器的URL,字符串的额外的实例失踪“/图/”被嵌入。

我试过了:

  • 使用之间切换ServerContextServerRequestRequestDispatcher
  • 使用绝对和相对路径(“./” - 是一个有效的相对路径?)
  • 安装(预申请中)contextPath中的URL

和其他各种黑客和回旋,但似乎没有任何使其工作; 它在不断变化发送给浏览器比出现在调试URL(与RD assoc'd)以外的内容的URL。

顺便说一句,该请求从(?引荐)源自localhost:8080/(context)/view/zzz.jsp ,贴到/AppControl ;

我使用的getServletContext()。的getRequestDispatcher(java.lang.String中)转发,所以我的印象是,目标资源是相对于上下文的根目录下。

这是我已经试过了,结果我有:

URL String Param     | Resulting URL
---------------------|---------------
 /view/xxx.jsp       | localhost:8080/view//view/xxx.jsp
 ./xxx.jsp           | localhost:8080/myApp/xxx.jsp
 ./view/xxx.jsp      | localhost:8080/view/view/xxx.jsp
 /myApp/view/xxx.jsp | localhost:8080/myApp/myApp/view/xxx.jsp

我会跟进这个职位与实际的代码,也许截图(如果我能做到这一点)......我的环境:

  • Java的6.0_34
  • Eclipse的朱诺(4.2.0)
  • Tomcat的7.0.21。

码:

            //get page navigation info
            setNavigation();
            session.setAttribute("viewControl", this.pageNext);

            //navigate to this.pageNext
            //response.sendRedirect(this.pageNext);
            pageForward(request, response, this.pageNext);
        }
    }else {
        pageForward(request, response, this.appStartPage);
    }
}

private void pageForward(HttpServletRequest request, HttpServletResponse response, String url) 
throws ServletException, IOException {
    RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
    //RequestDispatcher rd = request.getRequestDispatcher(url);

    rd.forward(request, response);
}

private void setNavigation() {
    //this.urlViewBase = this.urlAppBase + "/view/";
    this.urlViewBase = "/view/";
    //this.urlViewBase = "./";
    //this.urlViewBase = "./view/";

    if (this.pageCurrent.equals("StartView.jsp")) {
        this.pageNext = this.urlViewBase + "ApplicantInfo.jsp";
        this.pagePrevious = this.urlViewBase + "StartView.jsp";
    }
    else if (this.pageCurrent.equals("ApplicantInfo.jsp")) {
        this.pageNext = this.urlViewBase + "ApplicantAddress.jsp";
        this.pagePrevious = this.urlViewBase + "StartView.jsp";
    }
    else if (this.pageCurrent.equals("ApplicantAddress.jsp")) {
        this.pageNext = this.urlViewBase + "Menu.jsp";
        this.pagePrevious = this.urlViewBase + "ApplicantInfo.jsp";
    }
    else if (this.pageCurrent.equals("Menu.jsp")) {
        this.pageNext = this.urlViewBase + "Menu.jsp";
        this.pagePrevious = this.urlViewBase + "ApplicantAddress.jsp";
    }
    else {
        this.pageNext = this.urlViewBase + "StartView.jsp";
        this.pagePrevious = this.urlViewBase + "StartView.jsp";
    }
}
文章来源: Double forwarded ServletRequest using RequestDispatcher.forward() malforms URL and causes 404