Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContext and RequestDispatcher?

 
Shuaib Gill
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the relationship between ServletContext and RequestDispatcher? I found this code fragment doing some research.
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req,res);
Why do you need a ServletContext here?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 java.sun.com - deals with this organization.
Bill
------------------
author of:
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic