0

Problem with Setting Cookie

asked 2019-06-25 18:26:39 +0800

stan1996 gravatar image stan1996
1 1

Cookies, something comes, something does not come, I can not understand what the problem is - sometimes it can be cut from the third or fourth click, sometimes from the first, from what it can arise? I use tomkat 6.0.26

delete flag offensive retag edit

Comments

As you phrased your question I can't tell whether it's a tomcat or a ZK problem. Can you please provide an example of how you are trying to set the Cookie? And especially how this is click related?

cor3000 ( 2019-06-26 10:31:15 +0800 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2019-08-07 11:19:14 +0800

superium gravatar image superium
29 5

why not try the latest tomcat, and set cookies like this:

                    HttpServletResponse response = (HttpServletResponse) Executions.getCurrent().getNativeResponse();
                Cookie userCookie = new Cookie("user", username);
                userCookie.setMaxAge(604800);
                userCookie.setPath("/");
                response.addCookie(userCookie);
link publish delete flag offensive edit
0

answered 2019-09-11 22:12:24 +0800

zkulm gravatar image zkulm
100 2

That works for me as well!

Adding cookie - Example:

Cookie cookie = new Cookie("MyCookie", codedCookieString);                  
     cookie.setMaxAge(60*60*24*30); //store 30 days
     String cp = Executions.getCurrent().getContextPath();
     // if path is empty, cookie path will be request path, which could couse problems
     if(cp.length() == 0) {
        cp = "/";
     }
     cookie.setPath(cp);   
     //cookie.setDomain(pattern);         
     ((HttpServletResponse)Executions.getCurrent().getNativeResponse()).addCookie(cookie);

Reading cookies - Example:

Cookie[] cookies = ((HttpServletRequest) Executions.getCurrent().getNativeRequest()).getCookies();
  if (cookies != null) {
     for (Cookie cookie : cookies) {
        String cookieName = cookie.getName();
link publish delete flag offensive edit

Comments

how about: cookie.setMaxAge((int) Duration.ofDays(30).toSeconds()); avoiding those weird numbers 60x60x24x30 or 604800 and unnecessary comments

cor3000 ( 2019-09-12 11:17:25 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
2 followers

RSS

Stats

Asked: 2019-06-25 18:26:39 +0800

Seen: 17 times

Last updated: Sep 11 '19

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More