File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes ServletContext and RequestDispatcher? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContext and RequestDispatcher?" Watch "ServletContext and RequestDispatcher?" New topic

ServletContext and RequestDispatcher?

Shuaib Gill
Ranch Hand

Joined: May 29, 2001
Posts: 62
What is the relationship between ServletContext and RequestDispatcher? I found this code fragment doing some research.
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
Why do you need a ServletContext here?

William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
ServletContext holds all the information about the web application context your servlet is executing in. Any requests for servlet engine functions such as request dispatch have to go through this context.
Sun's design tries to make each web application independent from every other application the server may be dealing with. The servlet API - downloadable from - deals with this organization.
author of:
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
I'm just curious if anyone here has the servlet/jsp specifications with them. I have an application where the entire directory structure is pulled dynamically from some EJBs, that uses a servlet which matches a path something like this: /site/*
When I forward the request to a jsp page, the request parameters are forwarded, but the path info is not, and the uri is changed to point to the jsp page. I would think that forwarding would just pass the request and response as-is to the jsp page, but apparently it has other plans. This is in Tomcat. What should the 'official' behavior be?
I agree. Here's the link:
subject: ServletContext and RequestDispatcher?
It's not a secret anymore!