往cookie中写入中文
<%@ page language="java" contentType="text/html; charset=GBK" isErrorPage="true"%>
<jsp:directive.page import="java.net.URLEncoder"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<%
Cookie cookie = new Cookie("key",URLEncoder.encode("湖南涉外经济学院"));
cookie.setMaxAge(60*60*24*7);
response.addCookie(cookie);
%> <br>
<jsp:forward page="result.jsp"/>
</body>
</html>
从cookie中读取中文
<%@ page language="java" contentType="text/html; charset=GBK"%>
<jsp:directive.page import="java.net.URLDecoder" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'result.jsp' starting page</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();
if(cookies == null) out.println("读不到Cookie");
else {
for(int i = 0; i < cookies.length; i++) {
Cookie temp = cookies[i];
if(temp.getName().equals("key")) {
out.println(URLDecoder.decode(temp.getValue()));
}
}
}
%>
<br>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GBK" isErrorPage="true"%>
<jsp:directive.page import="java.net.URLEncoder"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<%
Cookie cookie = new Cookie("key",URLEncoder.encode("湖南涉外经济学院"));
cookie.setMaxAge(60*60*24*7);
response.addCookie(cookie);
%> <br>
<jsp:forward page="result.jsp"/>
</body>
</html>
从cookie中读取中文
<%@ page language="java" contentType="text/html; charset=GBK"%>
<jsp:directive.page import="java.net.URLDecoder" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'result.jsp' starting page</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();
if(cookies == null) out.println("读不到Cookie");
else {
for(int i = 0; i < cookies.length; i++) {
Cookie temp = cookies[i];
if(temp.getName().equals("key")) {
out.println(URLDecoder.decode(temp.getValue()));
}
}
}
%>
<br>
</body>
</html>