JSP:包括在使用 doPost requestDispatcher 的转发页面上不被调用

发布于 2024-10-01 13:00:45 字数 1527 浏览 4 评论 0原文

我目前正在编写一个网络应用程序,您需要使用用户名和密码登录。该过程的下一步是选择登录用户涉及的项目。

选择项目并单击提交按钮后,将调用 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 的语法在 doPostdoGet< 中完全相同/代码> 方法。

如果需要更多说明或额外代码。请告诉我。


编辑

使用的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 映射与 doGetdoPost 有什么关系呢?


亲切的问候,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅倚清风 2024-10-08 13:00:46

正如评论中提到的,监听 header.dofooter.do 的 servlet 似乎仅限于 GET 请求。您需要确保它也将在 POST 请求上执行。

关于评论中的新问题:

为什么当从另一个 jsp 页面调用 时会调用 doPost() 方法?

因为 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 and footer.do is restricted to GET requests only. You need to ensure that it is to be executed on POST requests as well.

As to the new question in the comment:

Why is the doPost() method called when a <jsp:include> is called from another jsp page?

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 the sendRedirect() who is doing that). The RequestDispatcher just reuses the initial request for the included/forwarded resources. The request method won't be changed and remains in this case POST 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 in service() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文