File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Redirecting to different pages using a servlet Controller Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Redirecting to different pages using a servlet Controller" Watch "Redirecting to different pages using a servlet Controller" New topic
Author

Redirecting to different pages using a servlet Controller

Peter Maxwell
Greenhorn

Joined: Jun 14, 2009
Posts: 14
G'day everyone,

I have four jsps say A,B,C,D and a servlet E.
I would like to have the servlet E display the page C if the request came from page A and page D if the request came from page B.
Is this possible/good idea?
If possible, is there any method available which can tell the servlet from where(which page) the request came?
If not/bad idea, what would be a good alternative?
Thanks.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Peter Maxwell wrote:
Is this possible/good idea?


Possible . It is not a bad idea, since servlet is a good controller. and you need to check the input parameters(which are from the jsps) in servlet using else if or switch
harilal ithikkat
Ranch Hand

Joined: Oct 06, 2008
Posts: 221
I think Request Dispatcher wil help you


SCJP 1.5
"A candle looses nothing by lighting another candle"
itechmentors.com
Peter Maxwell
Greenhorn

Joined: Jun 14, 2009
Posts: 14
Thanks seetharaman and harilal for quick replies.
seetharaman venkatasamy wrote:
Peter Maxwell wrote:
Is this possible/good idea?


Possible . It is not a bad idea, since servlet is a good controller. and you need to check the input parameters(which are from the jsps) in servlet using else if or switch


Is checking the input parameter is the only way to determine the source of the request or is there any specific method to do this?
I can always use a hidden parameter to differentiate between two pages when they have the same type/number of parameters(or both have no parameters) but it would be nice to know if there are any method specifically to deal with this.

Thanks again.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Peter Maxwell wrote:Thanks seetharaman and harilal for quick replies.

You are welcome

Peter Maxwell wrote:
Is checking the input parameter is the only way to determine the source of the request or is there any specific method to do this?
I can always use a hidden parameter to differentiate between two pages when they have the same type/number of parameters(or both have no parameters) but it would be nice to know if there are any method specifically to deal with this.


Alternate ways:

1.QueryString parameter

2. wild card servlet mapping
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60055
    
  65

First, why share the controller in this way? Why does each page not get its own controller? Do you have a good reason for the sharing?

Secondly, it's a bad idea to switch based upon implicit information such as where the page came from. This makes you code tightly coupled and fragile. Better to use explicit information such as a parameter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Peter Maxwell
Greenhorn

Joined: Jun 14, 2009
Posts: 14
Bear Bibeault wrote:First, why share the controller in this way? Why does each page not get its own controller? Do you have a good reason for the sharing?

Secondly, it's a bad idea to switch based upon implicit information such as where the page came from. This makes you code tightly coupled and fragile. Better to use explicit information such as a parameter.

Thanks Bear Bibeault for replying.
Well I thought it(sharing the controlloer) might save code and extra file for another servlet. Other than that i don't really have a good reason. So i'd be better off with a separate controller i guess.


Alternate ways:

1.QueryString parameter

2. wild card servlet mapping

Thanks Seetharaman,
I tried googling those terms but i couldn't get any pages explaining them.
Could you explain what they are?
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Redirecting to different pages using a servlet Controller
 
Similar Threads
Need help! Request Headers!
Doubts In IBM 287 Questions
Creating Dynamic JSP with XML
Some Tests
Weblogic 7.0 certification mock test question