aspose file tools*
The moose likes JSP and the fly likes Post-Redirect-Get pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Post-Redirect-Get pattern" Watch "Post-Redirect-Get pattern" New topic
Author

Post-Redirect-Get pattern

Clara Zetkin
Greenhorn

Joined: Dec 10, 2002
Posts: 19
Hi everyone,

I'm trying to use the PRG pattern as in http://www.javaranch.com/journal/200603/Journal200603.jsp#a5
I'm just not sure how to implement it. Are there any examples around?
How do I do the redirect from the task controller to the page controller as in Figure 4?

I just use beans and jsp - no serverfaces, servlets, struts or other whatdoyoucallthems. As I am new to the Java Web world, I thought it best to start simple.

Any links with code would be very helpful!

Clara
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61429
    
  67

Hi Clara, thanks for reading my article.

It sounds like you are programming using Model 1 (JSPs with beans, no servlets). In this case the JSPs are acting as their own controllers and patterns like PRG don't make a whole lot of sense.

The purpose of PRG is to separate task controllers from page controllers across a request boundary in order to avoid repeating the original POST operation. When it's all rolled up into a single JSP, rather than into separate units, there's really no way to create such a separation.

The only means of doing this in a non-servlet environment is to have the JSP that is being POSTED to just contain Java processing before redirecting to a JSP that composes the view, but that really makes no sense when a servlet is so much better suited for this purpose.

I would very much recommend that you avoid frameworks like JSF and Struts until you have a good grasp of JSP and servlets under your belt, but servlets themselves are a basic fundamental building block of web applications -- more fundamental than even JSPs -- and if you are at the point where you are wanting to employ patterns like PRG to create real-world web applications, then it's time to get servlets into your repetoire.
[ April 14, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Clara Zetkin
Greenhorn

Joined: Dec 10, 2002
Posts: 19
Hello Bear,

thanks for your reply. I'm using Model 2, with a Controller, who calls the different jsps. (Like "if (subaction != null && subaction.equals("addToDB")) ..". Is that a servlet? I'm getting confused by all these servlets, facelets, J2EE, EJB ... I haven't had the time to learn the vocabulary as well as the technology, so I concentrated on the latter :-)
And there's exactly the problem you describe with the reload button. So how to implement the two arrows from your Figure 4 from the page controller to the client (labelled "redirect") an from there to the page controller (labelled "request")?

Clara
[ April 18, 2006: Message edited by: Clara Zetkin ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Clara,

If you're not sure what a servlet is, you might want to pick up a book or search for some web tutorials.
http://www.google.com/search?hl=en&q=SERVLET+TUTORIAL&btnG=Google+Search

A servlet is a type of Java class.
JSPs get converted to servlets before being compiled.
I wouldn't worry about any of the other technologies you've mentioned yet.
All server side Java is build on top of servlets so learning them will give you a solid foundation to work from when tackling the others.

After learning servlets, JSP is a lot easier to understand as well.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Clara Zetkin
Greenhorn

Joined: Dec 10, 2002
Posts: 19
Hi Ben,

thanks for your reply. I actually did the first tutorial which comes up with that search some time ago - I'm just still unsure if just any code which runs on the server and processes web-requests is a servlet. I would call it a "java class". Well, I do use this class (which might be called a servlet) and try to split it into two classes (according to Bears article), but how do they call each other?
It must be something with redirect, I think - but there might be some code somewhere to help me? To add some more details: I have a form where depending on previous selections which option/select was choosen you get different fields to fill in. And finally a submit. I'm using JavaScript to fill 2 hidden fields, which give me the class on the server to call and a parameter to it. After processing it (depending on the users choice I have to get different data - filled in beans) and call the same jsp again. User makes some more selections and so on till submit. Should I use GET here?

Clara
[ April 18, 2006: Message edited by: Clara Zetkin ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm just still unsure if just any code which runs on the server and processes web-requests is a servlet. I would call it a "java class". Well, I do use this class (which might be called a servlet) and try to split it into two classes (according to Bears article), but how do they call each other?




In a nutshell, the servlet would instanciate the bean, call its methods, and bind it to one of the various scope objects (request, session, context).

It would then forward to the JSP for view formatting.
The JSP can either use the older <jsp:useBean .. /> syntax to declare a reference to the bean or it can use the newer JSTL/EL methods for locating and working with the bean.

I have a very simple example of this on my site, if you're interested.
http://simple.souther.us
Look for SimpleMVC.

The example doesn't use the Post-Redirect-Get (PRG) pattern but understanding the flow of a simple MVC app should make it easier to tackle.
[ April 18, 2006: Message edited by: Ben Souther ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61429
    
  67

Ben is, of course, right on the money. And his examples are well worth looking at.
Clara Zetkin
Greenhorn

Joined: Dec 10, 2002
Posts: 19
Thank you all for your replys. Could you still please point me to some code example for PRG Pattern?
Thanks!
Clara Zetkin
Greenhorn

Joined: Dec 10, 2002
Posts: 19
Hi again,

are there really no examples for this pattern around? Or is it so very obvious and it is just me who doesn't get it?
[ May 05, 2006: Message edited by: Clara Zetkin ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61429
    
  67

I don't have a simple example of non-proprietary code handy, but it really isn't rocket science. The typical scenario would go like this:

  • A form on a page is submitted to a task controller using the POST method.
  • The task controller does whatever its thing is, and then redirects to a page controller.
  • The page controller sets up whatever constructs are necessary for the page as scoped variables, usually in request scope.
  • The page controller forwards to the JSP to produce the resulting view.

  • Clara Zetkin
    Greenhorn

    Joined: Dec 10, 2002
    Posts: 19
    Thanks Bear,

    I was just wondering how to redirect and forward.
    httpServletResponse.sendRedirect("pageController.java")?
    And pageContext.forward("show.jsp")?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61429
        
      67

    There is no pageContext within a servlet. Look into the RequestDispatcher class.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Post-Redirect-Get pattern