File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Why is my RequestDispatcher null? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why is my RequestDispatcher null?" Watch "Why is my RequestDispatcher null?" New topic
Author

Why is my RequestDispatcher null?

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
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

Joined: Mar 22, 2000
Posts: 12784
    
    5
It seems likely that that string creates an invalid URL as far as the servlet context is concerned.
Bill
Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
It is not the URL, that is the problem.
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
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 ]

"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is my RequestDispatcher null?