• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is my RequestDispatcher null?

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WHy is that rd null? I printed if null then print something to the screen and sure enough it was null. Why?
public void service(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException

{
RequestDispatcher rd = getServletContext().getRequestDispatcher("mcserver_history.jsp");

HashMap hostnameMap = populateObject();
request.setAttribute("hostnameMap",hostnameMap);
rd.forward(request,response);
}
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems likely that that string creates an invalid URL as far as the servlet context is concerned.
Bill
 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not the URL, that is the problem.
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anthony
The following is lifted from the API docs for ServletContext.getRequestDispatcher():

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.

Your pathname does not begin with a "/" character - have you tried changing this?
[ October 24, 2003: Message edited by: Michael Fitzmaurice ]
[ October 24, 2003: Message edited by: Michael Fitzmaurice ]
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't like that, you should try the

request.getRequestDispatcher() method, where the API indicates a more forgiving attitude:

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic