• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

java.lang.IllegalArgumentException

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Won't you be my neighbor? - Fred Rogers. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic