Solved

Difference between getRequestDispatcher () of ServletContext and ServletRequest

Posted on 2004-09-02
6
1,803 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 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

19 Experts available now in Live!

Get 1:1 Help Now