File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes MVC, PRG and the double-post problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "MVC, PRG and the double-post problem" Watch "MVC, PRG and the double-post problem" New topic
Author

MVC, PRG and the double-post problem

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
Friends,

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?

Thanks,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60055
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60055
    
  65

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.


Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
I think I'm seeing it (lights are going on). The browser would display some page, which would include a POST action to our task controller. Let's call that URL "/task-controller".

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.

Thanks Bear!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60055
    
  65

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MVC, PRG and the double-post problem
 
Similar Threads
RequestDispatcher.forward()/include(), HttpServletResponse.sendRedirect() and HttpServlet.destroy()
Front Man Examples?
Accessing a variable from both EL and JSP Expression?
Post-Redirect-Get pattern
Which Method Process Forward