The session can only store attributes while the client is interacting with your application within the validity period of that session. In case of inactivity for a longer period than the validity or if the user closes the browser, the session is lost. You have already verified this by checking 2 different Jsessionids when closing and reopening the browser.
Therefore, if you only use the session to maintain the status, you will not be able to solve your problem.
To identify the customer only, a good option would be to use a cookie itself. The cookie is persisted in the customer’s own browser, usually in a text file, during the validity you specify.
In this case, you would need to set a specific cookie for your purpose. Your code would have to save the cart items in a database and assign a unique ID to that set of saved items. You could take advantage of JSESSIONID itself if you don’t want to generate a random string. Remember to update the database each time the user modifies something in the cart, putting or removing some product.
The cookie would be created and sent to the browser, having as content that unique ID. Adjust the validity of the cookie according to your business requirement. It would look like this:
String valorCookieCarrinho = ID; //O que você quiser definir, basta ser único
Cookie cookieCarrinho = new Cookie("cookieCarrinho_br.com.seusite", valorCookieCarrinho);
cookieCarrinho.setMaxAge(60*60*24*7); //Defina a validade - 1 semana?
response.addCookie(cookieCarrinho);
On every visit to the site, for which there is not yet an active cart you search for the cookie, get the ID and search for the corresponding items in the database. If they exist, you place them in the cart that is in the session. It is important to check first, not to risk overwriting an updated cart.
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("cookieCarrinho_br.com.seusite")) {
//pegar o valor do ID com cookie.getValue() e buscar no bd
}
}
}
Do not forget to have the database purge the abandoned records. For this, save the date on each cart record.
Yes, saved in the cookie some information for you to retrieve the shopping cart.. of course you should save it on the server side as well
– Ricardo Pontual
I tried to make an example using JSESSIONID, if I do not leave the browser it works well, look at the output: Session Id = D5913F3F699B66B8AFEDD32E108F9C92 //new JSESSIONID cookie = F688E7A616018963013062251D16A805 //previous session, but when I close the browser and open again it ignores the previous session look what comes out: Session Id = 944D48C7F4BE1F807B207CC9694A629E JSESSIONID cookie = 944D48C7F4BE1F807B207C9694A629E In this case it is a test I am doing, it will be if the problem is because in this case, I haven’t saved that session to the server yet?
– Edjane
You will not go back to previous session, closed the browser it got lost, you will start a new session. Your cookie may have some information that identifies the user, a token, id, etc., so you start a new session... of course the cart needs to be saved on the server side in order to recover. You can use a bank nosql as Mongodb to save the cart, and when effective save to your transactional bank
– Ricardo Pontual
When the user is logged in I do not have this problem, because save the item with the user’s identification, the problem is when the user is not logged in. It would be correct to make this identification by ip?
– Edjane
In this case you could generate a temporary identifier while not logged in. IP is not a good idea because if it is behind a proxy, you may end up reading only the IP of the company proxy for example, and if more of a user of the same company access the site, will have the same IP, which will be a problem (everyone will see the same cart)
– Ricardo Pontual
I thought the same thing, really ip is not a good idea
– Edjane