Reputation: 35
I am building 'Cart' page, I have list of items and buttons, which should save that specific item in Cookie object.
Here is the code I have which prints items to webpage
Cookie c;
while(rs.next())
{
out.println("<div id=\"aaa\">");
out.println("<div id=\"bbb\" style=\"background-image:url("+rs.getString("poster")+"); background-repeat: no-repeat; background-size:cover;\">"+"</div>");
out.println("<div id=\"ccc\">");
out.println("<h2>"+rs.getString("name")+"</h2>");
out.println("<p>Category: <em>" + rs.getString("Category")+"</em></p>");
out.println("<p>Size: "+rs.getDouble("size")+"</p>");
out.println("<p>Publisher: "+rs.getString("publisher")+"</p>");
out.println("<p>Price:"+rs.getDouble("price")+"</p>");
c = new Cookie(rs.getString("name"), Double.toString(rs.getDouble("price")));
out.println("<footer class=\"align-center\">");
out.println("<a href=\"Cart\" target=\"_blank\" class=\"button alt\">Open</a>");
out.println("</footer>");
out.println("</div>");
out.println("<div id=\"ddd\"></div>");
out.println("</div>");
out.println("<hr />");
}
what I want exactly is cookie object to be filled with item name and price after this button is pressed.
out.println("<footer class=\"align-center\">");
out.println("<a href=\"Cart\" target=\"_blank\" class=\"button alt\">Open</a>");
out.println("</footer>");
Upvotes: 1
Views: 557
Reputation: 28522
You can passed the value of specific product i.e name,price etc
by passing that value in <a href="">
and get that value in your cart
page and use request.getParameter("something")
to get value of that item passed and save in cookies
like below :
String name=rs.getString("name");
String price=Double.toString(rs.getDouble("price"));
out.println("<footer class=\"align-center\">");
//passing value in url
out.println("<a href=\"Cart?name="+name+"&price="+price\" target=\"_blank\" class=\"button alt\">Open</a>");
out.println("</footer>");
And then in your cart page
do like below :
String name=request.getParameter("name");//getting value from url
String price=request.getParameter("price");
Cookie ck=new Cookie("name",name);//creating cookie object name
response.addCookie(ck);//adding cookie in the response
Also ,don't use html
in your servlet instead put this in jsp
Upvotes: 1