File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Removing session attributes with regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Removing session attributes with regex" Watch "Removing session attributes with regex" New topic

Removing session attributes with regex

Kumara Sharma

Joined: Sep 29, 2010
Posts: 6
Hi All,

I need to remove all the session attributes based on a pattern match.
Ex: Attribute keys being: book_firefox, book_chrome, etc.

Is there a way to remove all the attributes that start with book_* in a single statement.
HttpSession.removeAttribute(String) takes a single string as parameter, so how to achieve this?
Harsha Ka
Ranch Hand

Joined: Mar 14, 2010
Posts: 45

One possible way is using getAttributeNames() to get all the objects and iterate over these and check the key, and if the object key is matching your regex validation, remove this object.

SCJP 1.6,Preparing (Tryin to prepare) for scwcd
I agree. Here's the link:
subject: Removing session attributes with regex
It's not a secret anymore!