Server-side Write cookie client does not get solution __java

Source: Internet
Author: User

Today found a problem, the program in the local run well, packaged to the server, no matter how to add a log, update packaging, and finally there are problems,

Locate the problem step-by-step, and discover that it is a cookie problem.

The service side defines the code that sets the cookie:

public void Setcookie (HttpServletRequest request, httpservletresponse response, String key,string value) {
        Cookie Cookie = new Cookie (key, value);//Create a Cookie,cookie name is key
        
        Cookie.setdomain (Request.getservername ());
        Cookie.setpath (Request.getcontextpath ());
        Set the validity period of the cookie
        cookie.setmaxage (constants.cookiemaxage); 
        Cookie.sethttponly (true);                 
        Adds a cookie object to the response object so that the server outputs the cookie to the client browser 
         response.addcookie (cookie) when it outputs the contents of the response object;
 

At first it was suspected that the cookie was not written on the server side, and later through the log, it found that the server was written, but the client could not get it. Used to do

The problem with cookies in web development is basically domain and path issues, but there is no problem with local running and the online environment is fine, I started

Pondering should be another problem, the expiration time problem.

We set the time is 43,200 seconds is 12 hours, but actually see into the first page, did write a cookie, but the expiration time is in the service

12 hours after the time, here is the point of the problem. The test server time is set ahead.

Finally found that the test environment of the machine has indeed been moved, the time was changed to the day earlier, and our cookie expires in 12 hours, even if

Is 12 hours, 43,200 seconds is also past the current time, then modify the test environment time for the current time is good, so that Tomcat does not have to reboot.

To modify the server time:

Note that if you set the HttpOnly property of the cookie to true on the server side, it will also cause the server to write a cookie, and the client will not be able to access the cookie.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.