*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher" Watch "RequestDispatcher" New topic
Author

RequestDispatcher

sri chitra
Greenhorn

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

res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("hello");
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:
hello

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" ?
thanks.
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
Greenhorn

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

www.LeadsAndDeals.com
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
Greenhorn

Joined: Feb 27, 2002
Posts: 5
Hi,
You should forward/include the response from the
servlet to which the request is dispatched.
forward(req,res)or include(req,res.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: RequestDispatcher
 
Similar Threads
Newbie: Need help with checkbox post parameters
RequestDispatcher.forward()
Requested Dispatcher
Request Dispatcher doubt
Hi.I need help