Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

am I being lied to about getRequestDispatcher()?

 
JohnWilliam Fitz
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

Two of my exam prep books, and the online API (from Sun) say that ServletRequest.getRequestDispatcher(String path) returns null if the path supplied to the method as an argument does not exist.

However, back in the real world, when I supply a bogus path such as I do below to getRequestDispatcher() Tomcat gives me back an org.apache.catalina.core.ApplicationDispatcher object!

So, are Sun and two publishers lying to me? Or is Tomcat violating the spec?

Here's the code...

javax.servlet.RequestDispatcher dispatch
= request.getRequestDispatcher("/i.dont.exist.jsp");
if (dispatch == null) {
System.out.println("\ndispatch is null");
} else {
System.out.println("\ndispatch is " + dispatch);
}

And here's the output it produces...

dispatch is org.apache.catalina.core.ApplicationDispatcher@a030d6
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

where did you read that this method returns null in case of a non-existing or invalid URL path? The API documentation from Sun says something slightly different about the return value. Have a look at this.

According to this documentation the method only returns null if the container is unable to return a RequestDispatcher - whatever this exactly means

Marco
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In practise, this has always returned null for me---it shouldn't really happen at all though as you'd have bug-checked your app. to ensure all dispatches are valid (an NPE in a production app. usually means I've done something wrong anyway!)

For getNamedDispatcher, the Servlet 2.4 spec states "If no servlet is associated with the given name, the method must return null." I imagine the same should hold true for getRequestDispatcher. So I guess whatever version of Tomcat you're on is not behaving as expected. Older versions of Tomcat definitely did return null though!

This really isn't important for the exam BTW.
[ November 30, 2008: Message edited by: Charles Lyons ]
 
JohnWilliam Fitz
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where did you read that this method returns null in case of a non-existing or invalid URL path?


Well, we won't name any names. But I assure you it is written in at least two test prep books. Or at least my little brain interpreted it as such.

Thanks for the help from both of you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic