When does a ServletContext return a null RequestDispatcher?

Api for ServletContext # getRequestDispatcher () says:

This method returns null if the ServletContext cannot return RequestDispatcher.

and

Returns: a RequestDispatcher object that acts as a wrapper for the resource at the specified path or null if the ServletContext cannot return RequestDispatcher

Why is ServletContext unable to return RequestDispatcher? At first I realized that if an invalid path were accepted, but it does not return null, this will lead to 404 in the browser.

I check the null value before calling the forward () method, and in the logs I see that after a while RequestDispatcher is null, but I donโ€™t know why, and I canโ€™t find out how to duplicate it.

thanks

Update

Like Fazal, I tried creating the RequestDispatcher at http://www.google.com/ to find out what would happen. This threw an IllegalArgumentException

java.lang.IllegalArgumentException: Path http://www.google.com/ does not start with a "/"

An exception was found in my try / catch block, so I had no way to check if RequestDispatcher was null or call the forward () method.

So, should there be another way for the ServletContext # getRequestDispatcher () method to return null without throwing an exception?

+4
source share
2 answers

Tomcat returns null for a path outside the current context, for example /../foo (but Jetty does not, so it is implementation specific).

+1
source

I saw this problem intermittently. But for me, this only happens when you try to switch to a valid patch that is not in the document root directory. For instance. Your server runs on http: // localhost / and I go to a URL like http://www.google.com . Not sure if you are facing this issue.

+1
source

All Articles