开发者

JSP:Include not being called on forwarding page using doPost requestDispatcher

开发者 https://www.devze.com 2023-01-24 14:52 出处:网络
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.

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 sele开发者_开发问答cted 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消