aspose file tools*
The moose likes Servlets and the fly likes Passing values between servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing values between servlets" Watch "Passing values between servlets" New topic
Author

Passing values between servlets

ford Darcy Jr
Ranch Hand

Joined: Jan 26, 2005
Posts: 76
Hi,

I need to pass the productid value from one servlet to the other. My productid value is a hyperlink and when the user clicks on the productid link it needs to call another servlet which has its corresponding description.

How do I pass values between servlets ??? Can anybody please help.

Thanks,
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Do you mean "Pass it from one servlet to another" as in Servlet Chaining or "Pass a parameter from the user's browser to a servlet"?

If it's the latter you could do so with a querystring variable.
You could also store it in the user's session (which is overkill if you only need it for that one page).


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Nikhil Menon
Ranch Hand

Joined: Nov 22, 2004
Posts: 70
My productid value is a hyperlink and when the user clicks on the productid link it needs to call another servlet which has its corresponding description.


Basically hyperlinks serve as GET request. So what you can do is to append the parameter, say productid to your href location seperated by '?'

It should look like

<a href = "/servlet1?product_id=value">click</a>

And from the servlet1 you override doGet() and inside you code request.getParameter("product_id"); Then do your logic.

Hope this is what you expected, if not give more details.

Nikhil Kanjulli Menon.
ford Darcy Jr
Ranch Hand

Joined: Jan 26, 2005
Posts: 76
Hi Nikhil,

Thanks a lot...but when I actually hardcode the productid value (just the way u mentioned) I am able to get the values. But when I reference it as productid ie <A HREF=servlet1?productid=productid>I am not getting any values. My application basically has to display the product details based on the productid which the user clicks.

Please help.

Thanks.
Nikhil Menon
Ranch Hand

Joined: Nov 22, 2004
Posts: 70
I guess you are using a jsp view, you can do it efficiently this way



<a href="Servlet1?product_id = <%= productid %>" > click </a>

Then in servlet1 you refer in doGet as
request.getParameter("product_id");

I think this should solve...

Nikhil
ford Darcy Jr
Ranch Hand

Joined: Jan 26, 2005
Posts: 76
Hi Nikhil,

I am not using JSP. Its just Servlets. But not getting the values.

Thanks.
Nikhil Menon
Ranch Hand

Joined: Nov 22, 2004
Posts: 70
In that case you have only servlets?? Well then it could be this way,

class: ViewServlet.java

out.println("<html>"+/*"......"*/);
.............
............
out.println("<a href=Servlet1?product_id=" +productid+ "> click </a>");
........
out.println("</html>");

This should work if your view was generated by your ViewServlet and the as shown it pass to Servlet1 with a parameter productid.

Try this out. And if it is not working please send your view's source code where you actually have to code <a href>.

Thank you.

Nikhil
ford Darcy Jr
Ranch Hand

Joined: Jan 26, 2005
Posts: 76
Hi Nikhil,

Thanks a lot. I was able to get the data populated.

Thanks for your time.
 
Don't get me started about those stupid light bulbs.
 
subject: Passing values between servlets