As part of exception handling, I want to print data from HTTP session like below:
try{
//business logic
} catch(Exception ex){
String user = session.get("userId"); //get user from HTTP Session.
log.info("Exception when processign the user "+user);
}
My question is do I get the correct UserId for which exception occurred since there will be mulitple threads updating the session?
The
HttpSessionis not shared among clients. So that part is safe already. The remnant depends on your own code as to obtaining and handling theHttpSessioninstance. If you’re for example assinging theHttpSessionas an instance variable of an application wide class, like the servlet itself, then it is indeed not thread safe as it might be overridden by another request at the moment you’re accessing it.See also: