aspose file tools*
The moose likes Servlets and the fly likes How do I make getRequest Dispatcher () return null? 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 » Servlets
Bookmark "How do I make getRequest Dispatcher () return null?" Watch "How do I make getRequest Dispatcher () return null?" New topic
Author

How do I make getRequest Dispatcher () return null?

Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Here's a snip from the API:The servlet receives as part of the URL, the 'next' page in the sequence. ie: servletName?nextPage=uiPage4.jsp

In the servlet code, I retrieve this parameter and do this:
But even if sendTo is some page that does not exist, disp will be non-null, and the forward will not fail as a 500 error, but rather a 404 error.

How do I trap the condition where someone tries to play monkey with the URL and requests a different page such as: servletName?nextPage=ImTryingToBeClever.jsp
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Problem is, there is a valid mapping for anything ending in .jsp: the JSP engine servlet. As a workaround, you could in case of .jsps use ServletContext.getResource() to see if the physical .jsp file is there before trying to get the request dispatcher.
- Peter
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Metadata to the rescue, mike! Be sneaky. Instead of:
servletName?nextPage=uiPage4.jsp
use servletName?nextPage=myFoo
Then have a hashmap that connects "abstract" page names to real page names. This is read out of an XML file or a properties file.
So your properties file would have:
myFoo=uiPage4.jsp.
That way you'll know instantly if the page they're requesting isn't a real page -- before you ever get a request dispatcher.
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Hi Peter and Kyle,

Thanks for your responses.

Peter, the solution you gave works very nicely. Thanks.

Kyle: I had actually thought of something very much like that. I coded that page way back at the start of development, and it's the only one that still uses the 'nextpage' parameter. I always thought it was kind of 'loose' to put the real page name right out there for all to see on the address bar. Laziness won out though.

Also, at this point, I'm doing minor tweaks and fixes to a 'deployed' application. So I need to furtively make small changes, test on my local instance of Tomcat, wait until everyone goes home, cross my fingers, and redploy on the deployment box.

Operations would have a fit.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Glad it worked. Kyle's solution would, obviously, have been the one to go for had it been feasible.
- Peter
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Well, there's always the next release
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Yes, the next release *sigh*. Kyle, do you know any devious tricks to persuade management to allocate time for refactoring? No matter how much I insist that it makes good business sense, short-termism rules and applications succumb to entropy well before their time.
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I make getRequest Dispatcher () return null?