Saturday, 23 April 2016

How many ways to get RequestDispatcher ?


RequestDispatcher.forward()
 is handled on the server, therefore the request and its associated session are available to the forwarded resource and you can pass data between them using request.setAttribute()

forward() does not involve the client's browser. It just takes browser's current request, and hands it off to another servlet/jsp to handle.
It forwards the ServletRequest and ServletResponse that it is passed to the path that was specified in getRequestDispatcher(String path).

The response will not be sent back to the client and the web container (For example, Tomcat) internally redirects the request to the other JSP/Servlet.
RequestDispatcher rd = request.getRequestDispatcher("sd.jsp");
rd.forward(request,response);

2 ways to get RequestDispatcher
1. Using ServletContext context.getRequestDispatcher("path")
    The pathname must begin with a "/" and is interpreted as relative to the current context root.

2. Using ServletRequest request.getRequestDispatcher("path");
    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.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.