• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RequestDispatcher

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try removing the & before item.
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic