Two Laptop Bag*
The moose likes Servlets and the fly likes A controller that forwards to different pages/controllers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "A controller that forwards to different pages/controllers" Watch "A controller that forwards to different pages/controllers" New topic
Author

A controller that forwards to different pages/controllers

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
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?


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
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

Joined: Sep 18, 2005
Posts: 746
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

Joined: Sep 18, 2005
Posts: 746
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)?
Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
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

Joined: Jan 25, 2005
Posts: 116
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A controller that forwards to different pages/controllers
 
Similar Threads
JSF check if message is empty
How can I access System Properties to determine a param-value for a context-param in Web.xml?
Integration with existing webapp
New to JForum
html:form not working if it is inside a jsp under WEB-INF