File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher" Watch "RequestDispatcher" New topic


sri chitra

Joined: Nov 16, 2000
Posts: 22
I am trying to call a servlet with parameter "item" and include the output to my outputstream.

PrintWriter out = res.getWriter();
RequestDispatcher rd = getServletContext().getRequestDispatcher("/servlets/xxxServlet?&item=xxx");
rd.include(req, res);
In the xxxServlet, Iam getting the parameter value for "item" using request.getParameter and printing it out as below:

PrintWriter out = res.getWriter();
out.println("item = "+request.getParameter("item"));
But I get the following output:

but not the output from the xxxServlet.
If I don't give any parameter, then I get the following output:
hello item=null
How can I get the proper output "hello item=xxx" ?
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Try removing the & before item.

Matthew Phillips
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
Hi - I tried it with and without the &.
I always get item=xxx.
I get item=null only if I do not code the item parameter at all.
I think it is a problem with the container's aggregation of parameters.
??? What container are you running?
Regards, Guy
[ March 08, 2002: Message edited by: Guy Allard ]
krishna valavala

Joined: Mar 06, 2002
Posts: 10
I tried this one myself. It appears that ampersand does not have any effect. I am always getting the correct parameter also. Only time I am not getting anything from the included servlet is when my path to the included servlet is not right. But since, you are getting item=null, this wouldn't apply. Also check to see if you have to manually restart the server (In case you do not have auto-reload feature) to see the changes
Vikas Aggarwal
Ranch Hand

Joined: Jun 22, 2001
Posts: 140
With RequestDispatcher you can forward/include the request. But it should always be done before any response committment to the browser. So if you are using out.println("hello"); before RequestDispatcher it will give exceptions and won't work properly. Try removing out.println("hello");

Vikas Aggarwal

Founder @
Leads and Deals Limited
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

vikinsa, that's not entirely accurate.

Only the RequestDispatcher.forward() will throw IllegalStateExceptions when the response is commited. There is not problem with using .include() when you have already performed output.
Sivanthi Thanikesavan

Joined: Feb 27, 2002
Posts: 5
You should forward/include the response from the
servlet to which the request is dispatched.
forward(req,res)or include(req,res.
I agree. Here's the link:
subject: RequestDispatcher
It's not a secret anymore!