The cookie contains problems caused by Chinese

Source: Internet
Author: User

Friday Project Test completed no problem after the online, after the online found that some accounts do not log on

As a result, cookies are included in the cookie used to record the tracking user. Read, an exception occurred while writing.

The exception is probably like this:

Java.lang.IllegalArgumentException:Control character in cookie value or attribute.
At Org.apache.tomcat.util.http.CookieSupport.isHttpSeparator (cookiesupport.java:193)
At Org.apache.tomcat.util.http.CookieSupport.isHttpToken (cookiesupport.java:217)
At Org.apache.tomcat.util.http.ServerCookie.appendCookieValue (servercookie.java:186)
At Org.apache.catalina.connector. Response.generatecookiestring (response.java:1032)
At Org.apache.catalina.connector. Response.addcookie (response.java:974)
At Org.apache.catalina.connector. Responsefacade.addcookie (responsefacade.java:381)
At Com.vcfilm.interceptor.service.AutologonService.setCookie (autologonservice.java:168)
At Com.vcfilm.interceptor.service.AutologonService.saveLogonInfo (autologonservice.java:129)
At Com.vcfilm.interceptor.service.AutologonService.saveLogonInfo (autologonservice.java:139)
At Com.vcfilm.wechat.actioncommon.BaseAction.SaveSession (baseaction.java:45)
At Com.vcfilm.wechat.actioncommon.BaseAction.SetMember (baseaction.java:191)
At Com.vcfilm.wechat.member.MemberAction.logincheck (memberaction.java:364)
At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:57)
At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43)
At Java.lang.reflect.Method.invoke (method.java:601)

Chinese need Urlencoder.encode. Utf-8

Can participate in this article http://blog.csdn.net/newyear1988/article/details/7817066

Added after the discovery still has a problem, write a cookie with DES encryption, found to take Cookie,getval () after the decryption of the time of the exception, so in the catch exception code block directly returned Getval () to get the value.

    /*** Take a value from the cookie **/     Publicstring Getcookieval (HttpServletRequest request, string key) {cookie[] cookies=request.getcookies (); if(NULL! = Cookies && cookies.length > 0){             for(Cookie c:cookies) {if(C.getname (). Equalsignorecase (Key)) {if(NULL!=c) {                        Try{                            returnUrldecoder.decode (C.getvalue (), "Utf-8"); }Catch(Exception e) {e.printstacktrace (); returnC.getvalue (); }                    }                }            }        }        return""; }        /*** Save value to Cookie **/     Public voidSetcookie (String key, String Val,intMaxAge) {        Try{val= Urlencoder.encode (val, "Utf-8")); }Catch(Exception e) {e.printstacktrace (); } Cookie Cookie=NewCookie (Key, Val); if(MaxAge > 0) {cookie.setmaxage (maxAge); } Cookie.setpath ("/");    Servletactioncontext.getresponse (). Addcookie (cookie); }

The cookie contains problems caused by Chinese

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.