If the processing of Chinese in jsp is a little bad, garbled characters may appear. Now I will introduce to you the implementation program of writing Chinese to cookies in jsp. Friends who need to know can refer to it.
Cookie cannot directly store Chinese, Chinese must be encoded into asccii strings, you can do this (http://www.javaweb.cc collection and sorting) The
code is as follows
String str = java.net.URLEncoder.encode(Chinese);
after getting the encoding The string is
taken out and decoded like this: the
code is as follows
String str = java.net.URLDecoder.decode (encoded string); the
specific method
is generally such a process of string transcoding == "stored in cookie ===" read Take cookie=="Transcoding=="Display the
first step: encode first when writing a cookie
The code is as follows
Cookie cookie=new Cookie("groupMembersList",URLEncoder.encode (groupMembersList,"GBK")); //The default is Temporary Cookie, MaxAge<0
//cookie.setMaxAge(-1);
response.addCookie(cookie);
Step 2: Read the cookie and then decode the
code as follows
<%
Cookie[] cookies=request.getCookies();
for( Cookie cookie:cookies){
if(cookie.getName().equalsIgnoreCase("groupMembersList"))
request.setAttribute("group",URLDecoder.decode(cookie.getValue(),"GBK"));
}
%>
<c:out value="${requestScope.group }"/>
还有一种办法
代码如下
try{
Cookie cookie = new Cookie(name, new String(value.getBytes("GBK"), "ISO8859-1"));
cookie.setDomain(this.domain);
cookie.setPath("/");
cookie.setMaxAge(120 * 1000);
response.addCookie(cookie);
}catch (Exception e){
e.printStackTrace();
}
The solution to writing Chinese garbled characters to cookies in JSP
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326976939&siteId=291194637
Recommended
Ranking