arttuladhar
7/31/2015 - 5:07 AM

Java cookie session

Java cookie session

String user_id = null;
String username = null;

//Check Cookie for the saved session
Cookie[] cookies = request.getCookies();
String _cookieName_id = "user_id";
String _cookieName_user = "user_name";
String _cookie_userid_Value = null;
String _cookie_usernameValue = null;

for (int i=0;i<cookies.length;i++){
Cookie cookie = cookies[i];
if (_cookieName_id.equals(cookie.getName()))
_cookie_userid_Value = cookie.getValue();
if (_cookieName_user.equals(cookie.getName()))
_cookie_usernameValue = cookie.getValue();
}

if (_cookie_userid_Value!= null && _cookie_usernameValue != null){
user_id = _cookie_userid_Value;
username = _cookie_usernameValue;
}