<%@page import="java.net.URLDecoder"%>
<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Cookie cookie=new Cookie(
URLEncoder.encode("姓名","UTF-8"),
URLEncoder.encode("刘金华","UTF-8")
);
response.addCookie(cookie);//发送到客户端
%>
<%
if(request.getCookies()!=null){
for(Cookie cc:request.getCookies()){
String cookieName=URLDecoder.decode(cc.getName(),"UTF-8");
String cookieValue=URLDecoder.decode(cc.getValue(),"UTF-8");
out.println(cookieName+"="+cookieValue+";<br/>");
}
}else{
out.println("Cookrie已经写入客户端,请刷新页面。");
}
%>
</body>
</html>
1.导包:URLDecoder,URLEncoder
2.保存到cookie中