1.创建一个动态Web本工程(2.5)版本的:
2.创建前端页面:index.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>
<!-- cookie的运行原理:
1.第一次向服务器发送请求时,在服务器端创建一个Cookie对象
2.将Cookie对象发送给浏览器
3.以后浏览器再发请求就会携带cookie对象
4.服务器根据不同的Cookie对象来区分不同的用户
-->
<a href="${pageContext.request.contextPath}/CreateCookie">创建Cookie对象</a><br>
<a href="${pageContext.request.contextPath}/GetCookie">获取Cookie对象</a><br>
<a href="${pageContext.request.contextPath}/PersistCookies">持久化Cookie对象</a><br>
</body>
</html>
2.创建对应的Servlet:
CreateCookie.java :这个是创建Cookie的Servlet
package com.hy.cookie;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CreateCookie extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*创建Cookie对象,名字不能使用中文Cookie对象的值可以使用中文,必须指定字符集进行编码
获取Cookie对象还需要指定字符集进行解码
*/
Cookie cookie = new Cookie("user","admin");
//只有当你访问pages/路径下的页面才会出现user2的Cookie
Cookie cookie2 = new Cookie("user2","pathCookie");
cookie2.setPath(request.getContextPath()+"/pages");
response.addCookie(cookie);
response.addCookie(cookie2);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
GetCookie.java :这个是获得Cookie
package com.hy.cookie;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetCookie extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if(cookies !=null) {
for(Cookie cookie :cookies) {
//获取Cookie对象的名字
String name = cookie.getName();
//获取Cookie对象的值
String value = cookie.getValue();
System.out.println("Cookie名字:"+name+","+"Cookie值:"+value);
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
PersistCookies.java :设置Cookie的存在时间
package com.hy.cookie;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PersistCookies extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie = new Cookie("user3","man");
/*持久化Cookie对象1min
* serMaxAge(int age)
*
* age>0:cookie对象Age秒后失效
* age=0:Cookie对象立即失效
* age<0:默认会话级别的Cookie对象
*/
cookie.setMaxAge(60);
response.addCookie(cookie);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}