javaweb中cookie用法

Cookie

一、向客戶端瀏覽器寫入Cookie

1)創建一個Cookie對象

Cookie cookie = new Cookie("name","admin");

2)setMaxAge:設置Cookie的最大時效,以秒爲單位;0,表示立即失效;負數,不存儲該Cookie;正數,表示存儲時間;

cookie.setMaxAge(30);

3)設置Cookie的作用範圍

cookie.setPath(request.getContextPath());

4)調用response的一個方法把Cookie傳給客戶端

response.addCookie(cookie);


二、從瀏覽器獲取Cookie


1)獲取Cookie

Cookie [] cookies = request.getCookies();

if(cookies != null && cookies.length >0 ){

for(Cookie cookie:cookies){

out.print(cookie.getName()+":"+cookie.getValue();

out.print("<>br");

}

}


Cookie 默認爲會話級別,通過setMaxAge設置變爲持久cookie


三、應用場景:

1)用戶登錄

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>cookie登錄頁面</title>
</head>
<body>
	<form action="index.jsp" method="post">
		<input type="text" name="name"><br>
		<input type="submit" value="submit">
	</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%
		//1.獲取name;
		String name = request.getParameter("name");
		//out.print(name);
		if (name != null && !name.trim().equals("")) {
			//2.儲存cookie
			Cookie cookie = new Cookie("name", name);
			cookie.setMaxAge(10);
			//3.將cookie傳給客戶端(ressponse方法)
			response.addCookie(cookie);
		} else {
			Cookie[] cookies = request.getCookies();
			if(name != null && !name.trim().equals("")){
				for(Cookie cookie : cookies){
					String cookieName = cookie.getName();
					if("name".equals(cookieName)){
						String value = cookie.getValue();
						name = value;
					}
				}
			}
		}
		if(name != null && !name.trim().equals("")){
			out.print("hello"+name);
		}else{
			response.sendRedirect("login.jsp");
		}
	%>
</body>
</html>

2)用戶瀏覽歷史記錄


<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h2>BOOK NOTE</h2>
	<h3>
		BOOK:<%=request.getParameter("book")%></h3>
	<h5>
		<a href="books.jsp" title="return">Reutrn BOOKS</a>
	</h5>
	<%
		String book = request.getParameter("book");
		
			Cookie [] cookies =request.getCookies();
			List<Cookie> bookCookies = new ArrayList<Cookie>();
			Cookie tempCookie = null;
			if(cookies != null&& cookies.length>0){
		for(Cookie c:cookies){
			String cookieName = c.getName();
			if(cookieName.startsWith("book_")){
				bookCookies.add(c);
				
				if(c.getValue().equals(book)){
					tempCookie=c;
				}
			}
		}
			}
			
			if(bookCookies.size()>=5 && tempCookie == null){
		tempCookie = bookCookies.get(0);
			}
		
			if(tempCookie!=null){
		tempCookie.setMaxAge(0);
		response.addCookie(tempCookie);
			}

			Cookie cookie = new Cookie("book_"+book,book);
			response.addCookie(cookie);
	%>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h2>BOOKS</h2>
	<a href="book.jsp?book=A">A</a>
	<br>
	<br>
	<a href="book.jsp?book=B">B</a>
	<br>
	<br>
	<a href="book.jsp?book=C">C</a>
	<br>
	<br>
	<a href="book.jsp?book=D">D</a>
	<br>
	<br>
	<a href="book.jsp?book=E">E</a>
	<br>
	<br>
	<a href="book.jsp?book=F">F</a>
	<br>
	<br>
	<a href="book.jsp?book=G">G</a>
	<br>
	<br>

	<%
		Cookie [] cookies = request.getCookies();
		if(cookies !=null && cookies.length>0){
			for(Cookie c:cookies){
			String cookieName = c.getName();
			if(cookieName.startsWith("book_")){
		out.println(c.getValue());
		out.print("<br>");
			}
			}
		}
	%>
</body>
</html>


發佈了35 篇原創文章 · 獲贊 15 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章