jQuery in Action, 3rd edition
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes getRequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "getRequestDispatcher" Watch "getRequestDispatcher" New topic


Ankur Jain Kothari
Ranch Hand

Joined: Feb 08, 2010
Posts: 154
why does the getRequestDispatcher of servletcontext allow only full path beginning with "/" ?

Do What You Wanna Be....Taking Things The Way They Come
scjp 1.6 91 percent, scmad 90 percent(rounded off to nearest integer)
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Ted.

I think(I am guessing) this is due to support for foreign context and to increase the performance of the application.

In our servlet we get ServletContext object like,
Here the ServletContext object belongs to our current application.
Suppose we want to get resource from another application from within our application.
This can be done using foreign context like

Here the getContext("/WebApp2") method creates the another ServletContext object(which we refer as foreign context) and sets the path(/WebApp2) in HttpServletRequestImpl object(which will be implemented by container/server) like

and it also provide another method to get the context name like,

Now our request object is having foreign context name. Now we want to get the resource from another application from within our application. This can be done by using RequestDispatcher like,
Here the getRequestDispatcher() method creates the container/server implemented RequestDispatcher object, i.e. it implements the RequestDispatcher interface and it might provide another method to store the resource. Like it wraps the resource in it.
Suppose that method is

and it also provide another method to get the resource like,

Now we are going to forward the resource to the application 'WebApp2". This can be done by using,

Here the actual usage of using ("/") comes in. Before forwarding the resouce to the application(WebApp2), first it has know complete path of the resource. Here the path is '/WebApp2/result.jsp'. The forward() might implement like this,

Now we got the actual resource name (/WebApp2/result.jsp).

Suppose if the getRequestDispatcher() wont made compulsary of using ("/") before the resource. We will get the actual resource using

Here we used the string "/" to get the actual resource. We know the "/" is placed in string pool, whenever the class is loaded by the JVM. This will not be garbage collected, even when GC thread executed. It will stay as long as the class is present in JVM. It will occupy memory resources.

I think because of the above disadvantage, Sun made compulsary of using ("/") before resource.
I agree. Here's the link: http://aspose.com/file-tools
subject: getRequestDispatcher
It's not a secret anymore!