2

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Rhizosis
  • 29
  • 2
  • 9
  • What servletcontainer are you using? How is the servlet on `*.do` configured in `web.xml`? – BalusC Nov 10 '10 at 14:07
  • I mean, the servlet which is listening on `header.do` and `footer.do`. The `*.do` is a typical Struts dispatcher servlet URL pattern. As far it sounds like that the servlet is restricted to GET requests only by its configuration in `web.xml`. – BalusC Nov 10 '10 at 14:47
  • Indeed. The Header.do and Footer.do are both java classes where in the `doGet` method the code is processed and printed. The `doPost` method is empty. Furthermore the `header.do` and the `footer.do` are configured in the same way as the ProjectSelect servletmapping mentioned in the code above. Do I need to call `doGet(req, res)` in the doPost methods of both these servlets (`Header.do` and `footer.do`)? – Rhizosis Nov 10 '10 at 14:56
  • That indeed did the job BalusC. Calling `doGet(request, response)` in the `doPost()` method did the job. Thank you very much for this insight. Question remains (to also help other people that might have run into this problem): Why is the `doPost()` method called when a is called from another jsp page? – Rhizosis Nov 10 '10 at 15:12

1 Answers1

1

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Much appreciated. Thank you very much. If you ever need help in bioinformatics (my field) do let me know :) – Rhizosis Nov 10 '10 at 16:17
  • You're welcome. I don't think so, I've a B.Sc in biomedical technologies which is similar. I'll however keep it in mind ;) – BalusC Nov 10 '10 at 17:05