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)