Ok, Two get you into the problem. I'm building I shopping cart. I have a proizvodid for product id variable and kolicina for quanity. I've tested the session and it stores all the things into Korpa bean and display it properly (commented code). Maybe its not the happiest idea as you can see but I imagined to make a string with all the product ids and prepare and execute a statement to show them. If someone is having a better idea I would gladly accept it, but for now I will go this way.
The code is
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(false);
ArrayList<beans.Korpa> korpa;
String akcija = request.getParameter("dodaj");
String proizvodid = request.getParameter("proizvodid");
String kolicina = request.getParameter("kolicina");
String productids = "";
if(session.getAttribute("korpa") == null) {
korpa = new ArrayList<beans.Korpa>();
session.setAttribute("korpa", korpa);
} else {
korpa = (ArrayList<beans.Korpa>)session.getAttribute("korpa");
session.setAttribute("korpa", korpa);
}
if(akcija != null) {
if(akcija.equals("Dodaj u korpu")) {
korpa = (ArrayList<beans.Korpa>)session.getAttribute("korpa");
korpa.add(new Korpa(proizvodid, kolicina));
}
}
//Filling the variable with product ids
for(int i=0; i<korpa.size(); i++) {
if(i != korpa.size() - 1) {
productids += korpa.get(i).getProizvodid() + ", ";
} else {
productids += korpa.get(i).getProizvodid();
}
}
if(productids != null) {
out.println(productids); //testing if ids are received correctly
}
try{
Connection con = DB.getInstance().getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM proizvod WHERE id IN (?)");
ps.setString(1, productids);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
out.println(rs.getString("proizvod")); //I just want to show results for now
}
} catch(Exception e) {
out.println(e.getMessage());
}
The problems comes when I run the web application, add first product to cart and it drags me to cart witch shows me the first added product id along with his name. I double check that, go to some other page and then go back and its still there. Then I try to add another product to cart and it add product id number in productids but doesn't show the name of that product.
Can someone point me what am I missing here? Thanks