The Cookie setting in Servlet is invalid.

Source: Internet
Author: User

The Cookie is used to save user information in the project. I didn't pay too much attention to it before. Today, how to set cookies is invalid, and the breakpoint has been followed countless times, and no problem is found, the code for sending the Cookie is executed, but the response cannot find where the Cookie is sent. The HttpWatch command also reads the Cookie multiple times, and the Response Header does not send the Cookie information.
I feel that my knowledge is too scarce and I have no way to start with any problems. This is nothing to do, with a try attitude to response. after the sendRedirect () Statement is moved to sending the Cookie, the miracle happens inadvertently. Then, I suddenly realized that the Cookie setting should be before the redirection, although no relevant information was found, but at least remember this conclusion first. Otherwise, you will have to take a detour. Today, you have to remember that it takes too much valuable time.
This should be avoided. The program will not report errors, but it will not get the expected results, which is crazy. Be very careful when writing code later. Mistakes are inevitable in the beginner's stage. Only when you see more can you be knowledgeable. More importantly, you can grow up only after constant accumulation. This process may be very painful, but it may also be painful and happy.

If ("true ". equals (request. getParameter ("auto_login") {Cookie cookie = new Cookie ("autoLogin", bean. getUsername () + "> <" + Encrypt. MD5 (bean. getPassword (); cookie. setMaxAge (3600*24*30); cookie. setPath ("/"); response. addCookie (cookie);} // the user name and password are correct. You can log on to the request. getSession (). setAttribute ("userbean", bean); response. sendRedirect (request. getContextPath () + "/");

 

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.