Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing values between servlets

 
ford Darcy Jr
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Nikhil Menon
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nikhil,

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

Thanks.
 
Nikhil Menon
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nikhil,

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

Thanks for your time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic