raushan
raushan

Reputation: 354

How I update database after session gets destroyed?

In my website I am creating a session when a user opens the home page. Now if he adds some items into the cart , items will be saved in a db table with session id and item id as a column. Now if someone closes his browser then items with those session IDs should get deleted. If I use sessionDestroyed() method then how can I get the session ID of the session which get destroyed ? Because I have to have the session id to delete the items from the table. Can I import HttpServletRequest in listener class ?

Upvotes: 2

Views: 243

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279960

The HttpSessionListener interface provides the method

public void sessionDestroyed(HttpSessionEvent sessionEvent)

which gives you access to a HttpSessionEvent object with a method

public HttpSession getSession()

that

Return the session that changed.

With the Session, you can then call getId() and use the returned ID to remove rows from your database.

Upvotes: 2

Related Questions