Cookie是服务器通知客户端保存键值对的一种技术
protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建Cookie对象,可以创建多个Cookie
Cookie cookie = new Cookie("key1","value1");
//通知客户端保存Cookie
resp.addCookie(cookie);
resp.getWriter().write("Cookie创建成功");
}
CookieUtils.java:
/**
* 通过指定key在指定Cookie数组中查找Cookie
* @param name
* @param cookies
* @return
*/
public static Cookie findCookie(String name, Cookie[] cookies) {
if (name == null || cookies == null || cookies.length == 0) {
return null;
}
for (Cookie cookie : cookies) {
if (name.equals(cookie.getName())) {
return cookie;
}
}
return null;
}
CookieServlet.java:
protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie[] cookies = req.getCookies();
Cookie needCookie = CookieUtils.findCookie("key1",cookies);
for (Cookie cookie : cookies) {
//getName()获取key
//getValue()获取value
resp.getWriter().write("Cookie[" + cookie.getName() + "=" + cookie.getValue() + "]
");
}
}
方式一:
protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1、先创建一个要修改的同名Cookie对象
Cookie cookie = new Cookie("key1","newValue1");
//2、在构造器,同时赋予新Cookie值。
resp.addCookie(cookie);
//3、调用response.addCookie(Cookie);通知客户端保存修改
resp.getWriter().write("key1已修改");
}
方式二:
protected void updateCookie2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1、先找到需要修改的Cookie对象
Cookie cookie = CookieUtils.findCookie("key1",req.getCookies());
if (cookie != null) {
//2、调用setValue()赋予新的Cookie值
cookie.setValue("newValue2");
//3、调用response.addCookie(Cookie);通知客户端保存修改
resp.addCookie(cookie);
}
}
Cookie生命控制指的是管理Cookie什么时候被销毁(删除)
setMaxAge()
删除Cookie对象,其它操作同理
protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//先找到Cookie对象
Cookie cookie = CookieUtils.findCookie("key1", req.getCookies());
if (cookie != null) {
//调用setMaxAge(0);
cookie.setMaxAge(0);
//调用response.addCookie(cookie)
resp.addCookie(cookie);
resp.getWriter().write("key1已删除");
}
}
Cookie的path属性可以有效过滤哪些Cookie可以发送给服务器,哪些不发。
path属性是通过请求的地址来进行有效的过滤
protected void setPath(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie = new Cookie("path1","path1");
//工程路径下的abc目录
cookie.setPath(req.getContextPath()+"/abc");
resp.addCookie(cookie);
resp.getWriter().write("已经创建了一个带有path路径的Cookie");
}
login.jsp
loginServlet.java
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
if ("admin".equals(username) && "123456".equals(password)) {
Cookie cookie = new Cookie("username",username);
cookie.setMaxAge(60*60*24*7); //保存七天
resp.addCookie(cookie);
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
}
Session是一种用来维护一个客户端和服务器之间关联的一种技术
request.getSession()
isNew():判断Session是否为刚创建出来的
getId():获取Session唯一的Id
protected void createOrGetSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建和获取Session会话对象
HttpSession session = req.getSession();
//判断当前Session会话是否为新创建的
boolean isNew = session.isNew();
//获取Session会话的唯一标识id
String id = session.getId();
resp.getWriter().write("得到的Session,它的id是" + id + "
");
resp.getWriter().write("这个Session是新创建的" + isNew + "
");
}
/**
* 往Session域中保存数据
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
protected void setAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getSession().setAttribute("key1", "value1");
resp.getWriter().write("已经往Session中存储了数据");
}
/**
* 获取Session域中的数据
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
protected void getAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object attribute = req.getSession().getAttribute("key1");
resp.getWriter().write("从Session获取出key1的数据是:" + attribute);
}
//单独设置超时时长,超过指定的时长,Session就会被销毁
public void setMaxInactiveInterval(int interval)
//获取Session的默认超时时长
public void getMaxInactiveInterval(int interval)
//让当前Session会话马上超时无效
public void invalidate()
30
protected void life3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.setMaxInactiveInterval(3);
resp.getWriter().write("当前Session已设置为3秒后超时");
}