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

java.lang.IllegalArgumentException

 
amarkirt saroay
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.IllegalArgumentException is thrown If the Path of the request.getrequestDispatcher("testabc.jsp") does not start with a "/" character .

Should not it throw IllegalStatexception?
 
Mateus Lucio
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IllegalArgumentException makes sense for me ... you see, getRequestDispatcher method takes a String argument that must start with a slash "/", so any String that doesn't start with a slash is an illegal argument.

Remember that this rule only applies when you get the RequestDispatcher from the ServletContext, in your example you got it from the HttpServletRequest so a String that doesn't start with a slash is a perfectly valid argument.

 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know its an old thread.
But my question is related to same thing somehow.
Even i got IllegalArgumentException in folllowing code:


But i could not find it in API.
In API it is written :
getRequestDispatcher
public RequestDispatcher getRequestDispatcher(java.lang.String path)Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.
The pathname must begin with a "/" and is interpreted as relative to the current context root. Use getContext to obtain a RequestDispatcher for resources in foreign contexts. This method returns null if the ServletContext cannot return a RequestDispatcher.

Parameters:
path - a String specifying the pathname to the resource
Returns:
a RequestDispatcher object that acts as a wrapper for the resource at the specified path, or null if the ServletContext cannot return a RequestDispatcher


Where is it written that IllegalArgumentException will be thrown?
 
Pete Pommelich
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

it depends if you get the RequestDisptacher from the ServletRequest or from the SverletContext. If you use the ServletContext, e.g.

the String has to start with a slash.
The pathname must begin with a "/" and is interpreted as relative to the current context root. Use getContext to obtain a RequestDispatcher for resources in foreign contexts. This method returns null if the ServletContext cannot return a RequestDispatcher.


If you use the request the String might start with a slash, but it doesn't have to (depending on the current dir etc.):

The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher.
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pete,
Thanks for your reply.I agree that we must use a "/" in case we are using ServletContext to get a requestDispatcher.
But i was not clear about what would happen if we try to use it like

So i tested it and got IllegalArgumentException.
But all that is written in api is
This method returns null if the ServletContext cannot return a RequestDispatcher.


There is no mention of IllegalArgumentException there.
Now i see many disparities in spec and containers.Thats why i asked this question here.
But anyway, i think the right answer is that it will throw IllegalArgumentException.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic