Cookie usage Issues in Java (message:invalid character [+] is present in the cookie value)

Source: Internet
Author: User

1, problem description

The following code is executed in the servlet:

 Public voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Text/html;charset=utf-8"); System.out.println (NewDate (). toString ()); Cookie Cookie=NewCookie ("Lasttime",NewDate (). toString ());        Response.addcookie (cookie); String s= "You are welcome to visit this website for the first time!" ~"; Cookie[] Cookies=request.getcookies (); if(Cookies! =NULL)             for(Cookie cs:cookies) {if(Cs.getname (). Equals ("Lasttime") ) {s= "The last time you logged in was:" + cs.getvalue (). Replace ("-", "");    }} response.getwriter (). print (s); }

Throws the following exception:

2, traced

It is strange to have the above problem, because the program compiles through, at least proves that there is no syntax error, according to the compiler hint, locate the problem to:

Cookie cookie = new Cookie ("Lasttime",  new Date (). toString ()); Response.addcookie (cookie);

View Javaee-api and find the following

Go back and look at the code and find

input:    New Date (). toString ());
output:   21:56:39 CST 2018

It is obvious that there are spaces in the output string, so the program will error and there are invalid characters.

3. Solutions

The solution to the problem is very simple, as long as there is no space in the string can be successful, the following will give a few specific solutions, the program is modified as follows:

Fayi

Idea: With "-" instead", then remember to change back, the program runs successfully.  Public voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Text/html;charset=utf-8"); System.out.println (NewDate (). toString ()); Cookie Cookie=NewCookie ("Lasttime",NewDate (). ToString (). Replace ("", "-")); Cookie.setmaxage (60*60);        Response.addcookie (cookie); String s= "You are welcome to visit this website for the first time!" ~"; Cookie[] Cookies=request.getcookies (); if(Cookies! =NULL)             for(Cookie cs:cookies) {if(Cs.getname (). Equals ("Lasttime") ) {s= "The last time you logged in was:" + cs.getvalue (). Replace ("-", "" ");    }} response.getwriter (). print (s); }

Law II

idea: URL encoding, and then put the encoded string into a cookie, then URL decoding can be.  Public voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException { Response.setcontenttype ("Text/html;charset=utf-8"); System. out. println (NewDate (). toString ()); Cookie Cookie=NewCookies ("Lasttime",Urlencoder.encode (new Date (). toString (), "UTF-8" )); Cookie.setmaxage ( -* -);        Response.addcookie (cookie); String s="you are welcome to visit this website for the first time! ~"; Cookie[] Cookies=request.getcookies (); if(Cookies! =NULL)             for(Cookie cs:cookies) {if(Cs.getname (). Equals ("Lasttime") ) {s="the last time you logged in was:"+Urldecoder.decode (Cs.getvalue (), "UTF-8" );    }} response.getwriter (). print (s); }

The program runs normally and meets the expected value.

Cookie usage Issues in Java (message:invalid character [+] is present in the cookie value)

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.