What is the use of RequestDispatcher in servlet?
Answer Posted / devendra.m
RequestDispacher:
RequestDispacher is a interface,which is used to dispach a
request from servlet to another servlet.
consider there are two servlets.one servlet is having
business logic and another servlet is having presentation
logic.if you want to achieve these two tasks,(like wizard
application) these can be done by using request dispacher.
which means that whenever we making a request to sevlet1
from the browser,we are getting response,insted of getting
the respose from the servlet1, we want to delegate the same
request from servlet1 to servlet2.
here in RequestDispacher there are two more methods are
there....i.e forward() and include().
if you used forward() only destination servlet(i.e last
servlet) is eligible for construting the browser output.
if you used include() every servlet is eligible to construt
the browser output.
RequestDispacher rd = request.getRequestDispachet("/usrl
pattern of second servlet");
rd.forward(request,response);
//so once executing the this stmt,which is forwarding the
same request to next servlet and displying only last servlet
out put.
1. There is no difference b/w forward() and include() incase
of parameters and attributes.
attributes means obj incase of j2ee.
before calling the rd.forward(req,res) method if you want to
add any data or objects to the next servlet we can use some
methods which are there in request,session and application
request.setAttribute(key,"value"); //add the obj to the next
servlet and this method returs void
request.getAttribute(key,); //get the object ...and this
method returs obj
request.removeAttribute(key); // inorder to remove a
perticular obj we can use this method and this metho returs
void.
2. but remember that we can add the attributes,modify the
attributes and also we can remove the attributes in servlets
(attributes means objects).
but in case of parameters we can not add,modify and remove
from the servlets we can only read the parameters.(this is
main diff)
3.entire requesrdispatch operation can be done in server
side,client doesn't know any thing abut this.
4. by using requestdispatch we can dispatch a request from
one servlet to another servlet within the application
only...we can not send a request from one servlet of one
application into another servlet of another
application...(this is also important).
if you have any doubts pls reach me mdevendra@gmail.com
| Is This Answer Correct ? | 5 Yes | 3 No |
Post New Answer View All Answers
Which java application server is the best?
What are the common methods that are included in the http servlet class?
What are the disadvantages of storing session state in cookies?
What is the effective way to make sure all the servlets are accessible only when user has a valid session?
Is servlet thread safe?
Explain the different ways for servlet authentication?
What are the jobs performed by servlets?
What are the types of Session Tracking ?
What are the supporting protocol by HttpServlet ?
What is the capacity the doget can send to the server?
What are different ways for servlet authentication?
Define the life cycle of a servlets.
Given the request path below, which are context path, servlet path and path info?
What is the main purpose of java servlets?
Why the container loads server at the application startup and how?