This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher" Watch "RequestDispatcher" New topic


jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
There are two ways of getting a requestDispatcher :

1. RequestDispatcher view = request.getRequestDispatcher("result.jsp");

Here i can also use RequestDispatcher view = request.getRequestDispatcher("/result.jsp");

2. RequestDispatcher view = getServletContext().getRequestDispatcher("/result.jsp");

Here can i use RequestDispatcher view = getServletContext().getRequestDispatcher("result.jsp") or would it give me an error..saying the location has to start from root ??;
fadi aboona
Ranch Hand

Joined: Apr 25, 2010
Posts: 71

you get a RequestDispatcher in two ways from the request or from the context. With the servlet context you MUST include the foward slash because you can't specify a path relative to the current resource (the one that received this request.

Hope this helps.
I agree. Here's the link:
subject: RequestDispatcher
Similar Threads
RequestDispatcher's getRequestDispatcher()
HTTP Status 500
damn the requestDispatcher
getRequestDispatcher error in servlets