使用RequestDispatcher.forward()双向转发的ServletRequest使URL格式错误并导致404

时间:2012-09-12 01:07:43

标签: java eclipse tomcat forward requestdispatcher

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

AppControl处理表单提交,然后将请求转发给ViewControl,后者确定处理了哪个页面并将请求重新转发给" next"页。

第一个转发(servlet之间)工作正常;但是第二个(从ViewControl到JSP页面)使URL变形,我得到了404。

servlet都映射到上下文的根,JSP文件位于名为/ view /

的子文件夹中

转发#1从/AppControl转到/ViewControl,转发#2从/ ViewControl转到/view/xxx.jsp,但浏览器中显示的是localhost:8080/view//view/xxx.jsp,这显然是ServerContext不是它应该去的地方。请注意,发送到浏览器的URL中缺少上下文,以及字符串" / view /"的额外实例。正在嵌入。

我试过了:

  • ServerRequest
  • 之间切换使用RequestDispatcherlocalhost:8080/(context)/view/zzz.jsp
  • 使用绝对和相对路径(" ./" - 是一个有效的相对路径?)
  • 将contextPath附加(预先挂起)到URL

以及各种其他的黑客和旋转,但似乎没有任何东西可以使它工作;它不断将发送到浏览器的URL更改为调试中出现的URL以外的其他内容(与RD关联)。

BTW,请求来自(推荐人?)/AppControl,张贴到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 ;

我使用getServletContext().getRequestDispatcher(java.lang.String)转发,因此我认为目标资源相对于上下文的根目录。

这是我尝试过的,以及我已经取得的成果:

            //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";
    }
}

我会用实际代码跟进这篇文章,也许是截图(如果我能做到的话)......我的环境:

  • Java 6.0_34
  • Eclipse Juno(4.2.0)
  • Tomcat 7.0.21。

代码:

{{1}}

0 个答案:

没有答案