Today I tried this kind of code... after logged in I removed all cookies with my FF browser, then I refreshed web page and I got NPE :P So I've been thinking is there a simple way to "restore" or whatever session if cookies were removed manually on the client or what is the most optimal way in this situation especially if there is some data (related to session attribute) is still in the servlet context scope?
For example if I have some id in session as
session.setAttribute("id","hello world");
...and I have code like a
String userID=null;
Cookie []cookies=req.getCookies();
for(Cookie cookie:cookies)
{
if(cookie.getName().equals("id")){userID=cookie.getValue();}
}
String id=session.getAttribute("id");
User user=((MyUsers)context.getAttribute("Users")).getUser(id);
how can I remove user (to avoid duplicates) if
- A) There is no cookies because they were removed manually
- B) and
session.getAttribute("id");
throws NPE?
Thanks