• 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

A controller that forwards to different pages/controllers

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, I was thinking of an elegant solution for this... I have a generic form that sets the timestamp sesseion variable(this is used in sql queries). Now... Since it's generic, it's used in different pages. I need to be returning to one of those pages once the timestamp is set. Ofcourse, I will also need to go into the controller of that particular page first.

I am thinking of creating multiple static variables like this...



and then having a select statement on my controller, I will get a request parameter value and depending on the value of the parameter, I will know which controller to forward to.

myapp/mycontroller?frompage=1

But this seems very unelegant...

You guys have any better solution?
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to return to the controller before forwarding to any of those 4 pages that you want to go to, depending on a request parameter that sounds more like a 'filter' mechanism to me.

I did something similar in one of my applications and had a filter which implements an interface ( with all the target pages listed as constants) and acted as the controller for all those pages, forwarding to a page based on a request parameter.
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why didn't I think of filters! Anyway... If I get the chance to modify my code, I will try your suggestion. I made a temporary solution where I made a bundle(properties file) and put all the KEY=VALUE pairs there and depending on the request parameter, I will get the value of that key like



Hmmm... But there really is a point with what you suggested... Thanks!
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm... Isn't a Filter called before the servlet? How can I make the filter forward to that controller ONLY if the servlet is finished with its part(e.g. doing database stuff)?
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With a Filter you can examine the Request object first. Then you call doFilter on the FilterChain object which will invoke the next item in the chain (your servlet in this case). When everything down the chain is done, it returns to the point in the Filter AFTER the doFilter call. At THAT point you get to mess around with the Response object.

So, as far as I can see, you could do something like:


[ October 10, 2006: Message edited by: Daniel Dalton ]
 
vishwanath nadimpally
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Timothy Sam:
Hmmm... Isn't a Filter called before the servlet? How can I make the filter forward to that controller ONLY if the servlet is finished with its part(e.g. doing database stuff)?



A filter is a sort of a servlet. In fact if you look at the doFilter() method it has a copy of the ServletRequest and ServletResponse objects, which you could use for your control. And also it can be mapped to a url in a much similar way as you map servlets.



and you could use the filter for security as well.
 
reply
    Bookmark Topic Watch Topic
  • New Topic