Commit 36eb42ab by Patryk Czarnik

koszyk - inicjalizacja w listenerze sesji

parent 8fe808df
...@@ -26,10 +26,6 @@ public class AddToBasket extends HttpServlet { ...@@ -26,10 +26,6 @@ public class AddToBasket extends HttpServlet {
HttpSession sesja = request.getSession(); HttpSession sesja = request.getSession();
Basket basket = (Basket)sesja.getAttribute("basket"); Basket basket = (Basket)sesja.getAttribute("basket");
if(basket == null) {
basket = new Basket();
sesja.setAttribute("basket", basket);
}
basket.addProduct(product); basket.addProduct(product);
} }
} catch(Exception e) { } catch(Exception e) {
......
package sklep.basket;
import jakarta.servlet.annotation.WebListener;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionEvent;
import jakarta.servlet.http.HttpSessionListener;
@WebListener
public class BasketInitialization implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
session.setMaxInactiveInterval(30); // sesja wygasa po pół minuty - tylko na potrzeby zajęć
session.setAttribute("basket", new Basket());
System.out.println("Początek sesji " + session.getId());
}
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("Koniec sesji " + se.getSession().getId());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment