我的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
RequestDispatcher
和localhost:8080/(context)/view/zzz.jsp
以及各种其他的黑客和旋转,但似乎没有任何东西可以使它工作;它不断将发送到浏览器的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";
}
}
我会用实际代码跟进这篇文章,也许是截图(如果我能做到的话)......我的环境:
代码:
{{1}}