Solved

Difference between getRequestDispatcher () of ServletContext and ServletRequest

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 61
xampp tool 12 56
java imports not found 4 17
web project error add remove 1 27
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)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

790 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