The session can only save attributes while the client is interacting with your application, within the validity period of that session. In case of inactivity for a period longer than the validity or if the user closes the browser, the session is lost. You have already found this yourself by checking 2 different JSESSIONIDs when closing and reopening the browser.
So if you only use the session to maintain the state, you will not be able to solve your problem.
To identify the client only, a good option would be to use a cookie. The cookie is persisted in the client's own browser, usually in a text file, for 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 to a database and assign a unique ID to that set of saved items. You could take advantage of JSESSIONID itself if you do not want to generate a random String. Remember to update the database each time the user modifies something in the cart by placing 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);
At each site visit, for which there is no active cart yet you search for the cookie, get the ID, and search for the corresponding items in the database. If they exist, you put them in the cart that is in the session. It's important to check first so you do not run the risk of overwriting an upgraded 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 make the database purge the abandoned records. To do this, save the date in each cart record.