Solved

Difference between getRequestDispatcher () of ServletContext and ServletRequest

Posted on 2004-09-02
6
1,815 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

730 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