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
How to rectify errors in java servlet while compilation?
What is a web application and what is it’s directory structure?
What is called servlet mapping?
What do you mean by the servlet chaining?
Is servlet synchronized?
What is string tokenizer?
What are common tasks performed by Servlet Container?
Which method of the httpservletrequest object is used?
How can we implement a jsp page?
What are the different session tracking techniques?
Name the packages that work with servlet?
What is servlet interface?
What is url encoding and url decoding
Explain web application directory arrangement?
Can we refresh servlet in client and server side automatically?