JSP:包括在使用 doPost requestDispatcher 的转发页面上不被调用
我目前正在编写一个网络应用程序,您需要使用用户名和密码登录。该过程的下一步是选择登录用户涉及的项目。
选择项目并单击提交按钮后,将调用 servlet 来准备所选项目并创建 requestDispatcher
和 .forward
将请求和响应发送到我的主页。
主页布局:
页眉 div:
<div><jsp:include page="header.do" flush="true"/></div>
正文 div:
<div> code that is present in the mainpage.jsp </div>
页脚 div:
<div><jsp:include page="footer.do" flush="true"/></div>
可以说这 3 个 div 组成了主页。
使用 requestDispatcher 转发页面后,我可以看到主页的内容。但是,
未加载(DIV 留空)。只有当我刷新页面(我假设doGet
)时,包含内容才会正确加载。
是否有办法让包含在 doPost requestDispatch 执行中加载?
**注意:requestDispatchers 的语法在 doPost
和 doGet< 中完全相同/代码> 方法。
如果需要更多说明或额外代码。请告诉我。
编辑
使用的Servlet容器: Tomcat 6.0
Web.xml:
<!--- Servlet Mapping for Project Selection Servlet-->
<servlet>
<servlet-name>ProjectSelect</servlet-name>
<servlet-class>MyProject.Login.ProjectSelect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProjectSelect</servlet-name>
<url-pattern>/ProjectSelect.do</url-pattern>
</servlet-mapping>
但是 servlet 映射与 doGet
和 doPost
有什么关系呢?
亲切的问候,
B.
I am currently writing an web application where you need to log in, using username and password. The next step in the process is to select a Project in which the logged in user is involved.
Once the project is selected and the submit button has been clicked, a servlet is called to prepare the selected project and create a requestDispatcher
and .forward
the req and resp to my mainpage.
The layout of the mainpage:
The header div:
<div><jsp:include page="header.do" flush="true"/></div>
The body div:
<div> code that is present in the mainpage.jsp </div>
The footer div:
<div><jsp:include page="footer.do" flush="true"/></div>
Lets say that these 3 divs make up the mainpage.
After forwarding the page with the requestDispatcher
I get to see the mainpage's content. However the <jsp:include>
's are not loaded (the DIV's are left empty). Only when I refresh the page (doGet
, I assume) the includes will load correctly.
Is there anyway to let the includes load on a doPost requestDispatch execution?
**Note: The syntax of the requestDispatchers is exactly the same in the doPost
and doGet
methods.
If more clarification is needed, or extra code. Please let me know.
EDIT
Servlet container used:
Tomcat 6.0
Web.xml:
<!--- Servlet Mapping for Project Selection Servlet-->
<servlet>
<servlet-name>ProjectSelect</servlet-name>
<servlet-class>MyProject.Login.ProjectSelect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProjectSelect</servlet-name>
<url-pattern>/ProjectSelect.do</url-pattern>
</servlet-mapping>
But what would the servlet mapping have to do with the doGet
and doPost
?
Kind regards,
B.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如评论中提到的,监听
header.do
和footer.do
的 servlet 似乎仅限于GET
请求。您需要确保它也将在POST
请求上执行。关于评论中的新问题:
因为 HTTP 请求的方法是由客户端帐户触发的。
RequestDispatcher
不会触发全新的 HTTP 请求等(只有sendRedirect()
正在这样做)。RequestDispatcher
只是重用对包含/转发的资源的初始请求。请求方法不会更改,在本例中仍保留在包含/转发的资源中的POST
中。也就是说,您可能希望将所有
*.do
servlet 重新设计/重构为单个中央前端控制器 servlet,该 servlet 具有在service()
方法中实现的必要逻辑以避免重复/样板混乱。或者更好的是,采用 JSF、Struts(2)、Spring-MVC 等 MVC 框架。有关更多详细信息,请查看 这个答案。As mentioned in the comments, it looked like that the servlet which is listening on
header.do
andfooter.do
is restricted toGET
requests only. You need to ensure that it is to be executed onPOST
requests as well.As to the new question in the comment:
Because the method of the HTTP request as fired by the client accounts. The
RequestDispatcher
doesn't fire a brand new HTTP request or so (it's only thesendRedirect()
who is doing that). TheRequestDispatcher
just reuses the initial request for the included/forwarded resources. The request method won't be changed and remains in this casePOST
in the included/forwarded resources.That said, you'd probably like to redesign/refactor all your
*.do
servlets to a single central front controller servlet which has the necessary logic implemented inservice()
method to avoid duplicated/boilerplate clutter. Or even better, adopt a MVC framework like JSF, Struts(2), Spring-MVC, etc. For more detail, check this answer.