• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Post-Redirect-Get pattern

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Clara Zetkin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Clara Zetkin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ben is, of course, right on the money. And his examples are well worth looking at.
 
Clara Zetkin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your replys. Could you still please point me to some code example for PRG Pattern?
Thanks!
 
Clara Zetkin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Bear,

    I was just wondering how to redirect and forward.
    httpServletResponse.sendRedirect("pageController.java")?
    And pageContext.forward("show.jsp")?
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There is no pageContext within a servlet. Look into the RequestDispatcher class.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic