I've been doing a lot of reading about the MVC design pattern (how it pertains to Servlets and JavaServer Pages) as well as the double post problem and the Post-Redirect-Get design pattern to work around it.
I really like the way I can forward from the Servlet (controller) to the JSP (View) and it maintains the original URL. Also, this makes it possible to forward to a JSP page which is in the WEB-INF directory, essentially hiding it from the world.
However, it seems I can't hide the JSP page in this way when using the PRG design pattern, as I need to redirect to a publicly-visible JSP page.
I would like to be able to use the PRG design pattern (to avoid the double-post issue), but keep the visible URL unchanged. I was thinking I could do the following:
Have the servlet implement different doGet and doPost methods:
- doGet would create the session Bean (if not already created), then forward to the View JSP located in the WEB-INF directory;
- doPost would accept the request from the JSP page, process the request parameters, execute the business logic, then, if it wants to go back to the same JSP page, do a redirect to our servlet - which will end up executing the doGet method, starting the process all over again.
Is there an easier way to do this, or is my solution reasonable?
Bear Bibeault wrote:
Mark E Hansen wrote:However, it seems I can't hide the JSP page in this way when using the PRG design pattern, as I need to redirect to a publicly-visible JSP page.
Nonsense. You redirect to the page controller of the JSP. The JSP remains hidden.
Thanks, Bear. Are you saying I can use response.sendRedirect() to a page with a URL like "WEB-INF/path/mypage.jsp"?
I think I'm misunderstanding something. I thought when I use response.sendRedirect, a response goes to the client, who then sends a new request using the provided URL, and that new URL would show in the URL bar of the client. And that the client can't make requests to any content under WEB-INF.
Mark E Hansen wrote:Are you saying I can use response.sendRedirect() to a page with a URL like "WEB-INF/path/mypage.jsp"?
No. I'm saying you should never directly hit a JSP page, you should hit its controller.
Don't confuse the difference between task and page controllers.
The task controllers do something, the page controllers prepare a page for view.
See the following diagram from this article:
The task controller redirects to the page controller, not the JSP.
The task controller would process all the request parameters, etc., then send a redirect to the page controller, which is the URL the user would see. We'll call that "/page-controller".
The page controller would set up the display Bean (in the session or wherever) and forward to the JSP page, which can live in "/WEB-INF". Because this is a forward, the user would still see the page-controller URL. This page would include a POST action to the original task-controller, and it all goes on from there.
Is that right? I think I've read that article three or four times, but didn't understand what was going on well enough to pull this out of it. I think I'll go read it again.