Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher

 
sri chitra
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try removing the & before item.
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You should forward/include the response from the
servlet to which the request is dispatched.
forward(req,res)or include(req,res.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic