lundi 3 mai 2021

How To Retrieve Cookies From Browser

Question: While I can set cookies named for cookemail, cookpassword and lastly cookrem in the browser, I cannot retrieve them from broswer. I can do all things in localhost side. Normally, these values are retrieved from browser when I run it on the server(tomcat) every time but the server always sees JSESSIONID not see other these values set into the browser.

    Cookie cEmail = new Cookie("cookemail", email.trim());
Cookie cPassword = new Cookie("cookpassword", password.trim());
Cookie cRemember = new Cookie("cookrem", remember.trim());
cEmail.setMaxAge(60 * 60 * 24 * 15);//15 days
cPassword.setMaxAge(60 * 60 * 24 * 15); //15 days
cRemember.setMaxAge(60 * 60 * 24 * 15); //15 days
response.addCookie(cEmail);
response.addCookie(cPassword);
response.addCookie(cRemember);

Retrive the cookies from browser (At the end of code snippet -> email = "", password = "",rememberVal="")

Cookie[] cookies = httpRequest.getCookies();

    String email = "", password = "",rememberVal="";
            if (cookies != null) {
                 for (Cookie cookie : cookies) {
                   if("cookemail".equals(cookie.getName())) { 
                     email = cookie.getValue();
                   }
                   if("cookpassword".equals(cookie.getName())){ 
                     password = cookie.getValue();
                   }
                   if("cookrem".equals(cookie.getName())){ 
                     rememberVal = cookie.getValue();
                   }
                }
            }



Aucun commentaire:

Enregistrer un commentaire