Java_Problem
asked on
Difference between getRequestDispatcher () of ServletContext and ServletRequest
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
From the javadoc of ServletRequest.getRequestD ispatcher( 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.getRequestD ispatcher( 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 "/".
'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.getRequestD
a relative path.'
So, if you get your RequestDispatcher from the request, you don't need
the leading "/".
thanks
doPost{}{
RequestDispatcher showDispatcher = req.getRequestDispatcher("
resp.setContentType("text/
PrintWriter out = resp.getWriter();
showDispatcher.forward(req
}
Final url on your browser is http://localhost:8080/servlet/A
ServletA
doPost{}{
RequestDispatcher showDispatcher = req.getRequestDispatcher("
resp.setContentType("text/
PrintWriter out = resp.getWriter();
showDispatcher.include(req
}
Final url on your browser is http://localhost:8080/servlet/A
ServletA
doPost{}{
resp.setContentType("text/
PrintWriter out = resp.getWriter();
res.sendRedirect("fwdinclu
}
Final url is http://localhost:8080/servlet/fwdinclude