JavaWeb----Cookie的两个应用

1.利用Cookie实现最近浏览的商品

books.jsp

<%@ 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>
<h4>商品列表</h4>

<a href="book.jsp?bookName=java">Java</a><br><br>
<a href="book.jsp?bookName=ajax">Ajax</a><br><br>
<a href="book.jsp?bookName=html">HTML</a><br><br>
<a href="book.jsp?bookName=javascript">JavaScript</a><br><br>
<a href="book.jsp?bookName=spring">Spring</a><br><br>
<a href="book.jsp?bookName=php">PHP</a><br><br>
<a href="book.jsp?bookName=android">Android</a><br><br>

<hr /><br><br>
<h4>显示最近浏览的商品</h4>
<% 
	//显示最近浏览的商品
	//获取所有的Cookie
	Cookie [] cookies = request.getCookies();
	if(cookies != null && cookies.length > 0)
	{
		//从中筛选出cookie name是“Limbo's”的书籍
		//显示cookie的value
		for(Cookie c : cookies)
		{
			if(c.getName().startsWith("Limbo_s_"))
			{
				out.println(c.getValue());
				out.println("<br>");
			}
		}
	}
%>
</body>
</html>
book.jsp

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ 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>
<h4>商品详情页</h4>
Book:<%= request.getParameter("bookName") %>
<a href="books.jsp">return</a>
<body>
	<%
		String bookName = request.getParameter("bookName");
		//把书的信息以Cookie的形式回传给浏览器,删除一个Cookie
		//1.确定要删除的Cookie:以Limbo	's开头的Cookie
		Cookie[] cookies = request.getCookies();
		//存放所有以Limbo's开头的cookie
		List<Cookie> cookieList = new ArrayList<Cookie>();
		//存放和book.jsp传入的book匹配的Cookie
		Cookie tempCookie = null;
		if (cookies != null && cookies.length > 0) {
			for(Cookie c : cookies)
			{
				String cookieName = c.getName();
				if(cookieName.startsWith("Limbo_s_"))
				{
					cookieList.add(c);
				}
				if(c.getValue().equals(bookName))
				{
					tempCookie = c;
				}
			}
		}
		//数量大于等于5,或者是books.jsp中传入的book不在Cookie中,
		//则删除数组中最早的那个
		if(cookieList.size() >= 5 && tempCookie == null)
		{
			tempCookie = cookieList.get(0);
		}
		if(tempCookie != null)
		{
			tempCookie.setMaxAge(0);   //设置最大时间为0相当于删除
			response.addCookie(tempCookie); //设置完了之后要回传,否则不会删除的
		}
		//将book.jsp封装成一个cookie回传
		Cookie cookie = new Cookie("Limbo_s_" + bookName, bookName);
		response.addCookie(cookie);
	%>
</body>
</html>

2.利用Cookie实现自动登录

login.jsp

<%@ 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>
	<form action="cookie.jsp" method="post">
		<input type = "text" name = "name"/>
		<input type = "submit" value = "submit"/>
	</form>
</body>
</html>
cookie.jsp

<%@ 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>
	<%
		String name = request.getParameter("name");
		//可以获取登录者的名字,即通过登录到达指定页面
		if(name != null && !name.trim().equals(""))
		{
			Cookie cookie = new Cookie("username" , name);
			cookie.setMaxAge(30);//设置cookie的时效,让cookie在30秒内有效,即30秒内无需重复登录
			response.addCookie(cookie);//返回cookie
		}
		//之间访问指定页面
		else{
			Cookie[] cookies = request.getCookies();
			if(cookies != null && cookies.length > 0 )
			{
				for(Cookie cookie : cookies)
				{
					String cookieName = cookie.getName();
					//匹配键值
					if("username".equals(cookieName))
					{
						name = cookie.getValue();
					}
				}
			}
		}
		
		if(name != null && !name.trim().equals(""))
		{
			out.println("hello" + name);
		}
		else
		{
			response.sendRedirect("login.jsp");
		}
	%>
</body>
</html>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值