Solved

Difference between getRequestDispatcher () of ServletContext and ServletRequest

Posted on 2004-09-02
6
1,805 Views
Last Modified: 2008-01-09
What is the difference between getRequestDispatcher () of ServletContext and ServletRequest

Please explain in easy to understand language and explain with a simple working example.

Please explain from latest J2EE version.

I want to understand when to use ServletContext and when to use ServletRequest's dispatcher call
0
Comment
Question by:Java_Problem
  • 4
6 Comments
 
LVL 35

Assisted Solution

by:girionis
girionis earned 25 total points
ID: 11966847
If you use the getRequestDispatcher() of the ServletContext then the dispatch you do is relative to the root of the ServletContext. If you use the getRequestDispatcher() of the ServletRequest then the dispatch you do is relative to the current HTTP request.

With the dispatch of the ServletRequest you do for example: request.getRequestDispatcher("/hello.jsp");
while with the ServletContext yo do: getServletContext().getRequestDispatcher("/yourWebAppContext/hello.jsp")

(from: http://www.theserverside.com/discussions/thread.tss?thread_id=21543)
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 25 total points
ID: 11966880
If you are usingn a named dispatcher (by calling SerlvetContext.getNamedDispatcher("some name")) and including the request, the behaviour must be:
none of the request attributes ("javax.servlet.include...")
must be set, and the paths (servletPath, requestURI...) remains as they were.

if you are forwarding to another resource
none of the request attributes ("javax.servlet.forward...")
must be set, and the paths (servletPath,requestURI...) must be ajusted

else
if you are not using a named dispatcher ( by calling ServletContext.getRequestDispatcher or ServletRequest.getRequestDispatcher) and including another resource:
the request attributes ("javax.servlet.include...") must be set,
and paths (servletPath,uri...,querystring) remains as they were

if you are forwarding to another resource
the request attributes ("javax.servlet.forward...") must be set,
must be set, and the paths (servletPath,requestURI...) must be ajusted


Notice that ServletRequest.getRequestURL() never changes
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11966902
ServletA
doPost{}{
RequestDispatcher showDispatcher = req.getRequestDispatcher("fwdinclude");
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
showDispatcher.forward(req,resp);
}

Final url on your browser is http://localhost:8080/servlet/A

ServletA
doPost{}{
RequestDispatcher showDispatcher = req.getRequestDispatcher("fwdinclude");
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
showDispatcher.include(req,resp);
}

Final url  on  your browser is http://localhost:8080/servlet/A

ServletA
doPost{}{

resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
res.sendRedirect("fwdinclude");
}

Final url is http://localhost:8080/servlet/fwdinclude
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11967010
From the javadoc of ServletRequest.getRequestDispatcher(String):

'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. This method returns
null if the servlet container cannot return a RequestDispatcher.

The difference between this method and
ServletContext.getRequestDispatcher(String) is that this method can take
a relative path.'

So, if you get your RequestDispatcher from the request, you don't need
the leading "/".
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12241138
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
for i loop in grovy 1 46
Java asynchronous logging 4 50
@SBGen Method 3 38
Why my table column Id is not passed to java object? 4 26
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now