版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
一、cookie的基本架构
1.交互页面
1.姓名–文本框
2.性别–单选框
3.爱好–复选框(用数组存放)
4.颜色–菜单栏
5.提交按钮
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'usingCookie.html' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="2.usingCookie.jsp" method="post">
<table border="1">
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>性别:</td>
<td>
男<input type="radio" name="sex" value="M" checked>
女<input type="radio" name="sex" value="F">
</td>
</tr>
<tr>
<td>爱好</td>
<td>
<input type="checkbox" name="Habit" value="阅读">
看书
<input type="checkbox" name="Habit" value="跑步">
跑步
<input type="checkbox" name="Habit" value="音乐">
音乐
</td>
</tr>
<tr>
<td>喜好颜色:</td>
<td>
<select size="1" name="color">
<option selected>none</option>
<option value="蓝色">蓝色</option>
<option value="绿色">绿色</option>
<option value="红色">红色</option>
<option value="黄色">黄色</option>
</select>
</td>
<td colspan="2" align="center">
<input type="submit" value="发送资料">
</td>
</tr>
</table>
</form>
</body>
</html>
2.处理页面
1.getParameter获取提交表单信息
2.保存到Cookie对象
3.添加到response
4.response转跳
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
request.setCharacterEncoding("gbk");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'usingCookie.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String strnames=request.getParameter("name");
String strname=java.net.URLEncoder.encode(strnames,"utf-8");
String strsex=request.getParameter("sex");
String strcolors=request.getParameter("color");
String strcolor=java.net.URLEncoder.encode(strcolors,"utf-8");
String[] strHabits=request.getParameterValues("Habit");
String strHabit="";
String t;
for(int i=0;i<strHabits.length;i++)
{
t=java.net.URLEncoder.encode(strHabits[i],"utf-8");
strHabit+=t+" ";
}
Cookie nameCookie=new Cookie("name",strname);
Cookie sexCookie=new Cookie("sex",strsex);
Cookie colorCookie=new Cookie("color",strcolor);
Cookie HabitCookie=new Cookie("Habit",strHabit);
response.addCookie(nameCookie);
response.addCookie(sexCookie);
response.addCookie(colorCookie);
response.addCookie(HabitCookie);
response.sendRedirect("3.responseCookie.jsp");
%>
</body>
</html>
3.展示页面
1.判断cookie是否为null
2.依次获取判断获取对应的cookie值
3.展示到页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'responseCookie.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Cookie cookies[]=request.getCookies();
if(cookies==null)
out.print("没有Cookie");
else{
try{
if(cookies.length==0)
{
System.out.println("客户端禁止写入Cookie");
}
else
{
int count=cookies.length;
String name="",sex="",color="",Habit="";
for(int i=0;i<count;i++)
if(cookies[i].getName().equals("name"))
name=cookies[i].getValue();
else
if(cookies[i].getName().equals("sex"))
sex=cookies[i].getValue();
else
if(cookies[i].getName().equals("color"))
color=cookies[i].getValue();
else
if(cookies[i].getName().equals("Habit"))
Habit=cookies[i].getValue();
%>
<font color="<%=color%>" size="5"><%=java.net.URLDecoder.decode(name,"utf-8")%></font>
的个人信息
<p>
<%
out.println("性别:");
if(sex.equals("M"))
out.println("男");
else
out.println("女");
out.println("<br> 我喜欢的颜色是:");
out.println(java.net.URLDecoder.decode(color,"utf-8"));
out.println("<br> 我的爱好是:");
out.println(java.net.URLDecoder.decode(Habit,"utf-8"));
}
}
catch(Exception e){
System.out.println(e);
}
}
%>
</p>
</body>
</html>