JavaWeb-Cookie和Session

Cookie和Session

  • 无状态协议:Http是无状态协议,是指当浏览器发送请求给server的时候,server响应,可是同一个浏览器再发送请求给server的时候,他也会响应,可是他不知道你就是刚才那个浏览器,简单地说就是server不会去记得你,所以是无状态协议。而DNS是有状态协议 。
  • 会话技术
    1、为了解决无状态的的弊端,因此需要会话技术来使服务器区分客户端。
    2、从打开一个浏览器访问某个站点,到关闭这个浏览器的整个过程,成为一次会话。

Cookie的处理

  • 服务器向客户端发送Cookie
  • 服务器接受客户端的Cookie

创建Cookie:
Cookie cookie = new Cookie(String cookieName,String cookieValue);

注意!Cookie中不能存放中文

|方法|说明| |---|---| |cookie.setMaxAge(int seconds)|设置持久化时间后Cookie会存储到磁盘文件中,时间到即销毁;不设置时间Cookie存放在内存中,会话结束Cookie销毁| |cookie.setPath(String path)|设置在哪些路径中会携带Cookie信息| |response.addCookie(Cookie cookie)|向客户端发送cookie| **删除Cookie**:使用同名同路径的持久化时间为0的cookie进行覆盖即可(将持久化时间设置为0) **服务器端接收客户端携带的Cookie** > * 在请求时cookie信息放到请求头中发送到客户端: 1、通过request获得所有的Cookie: `Cookie[] cookies = request.getCookies();` 2、遍历Cookie数组,通过Cookie的名称获得我们想要的Cookie ```java for(Cookie cookie : cookies){ if(cookie.getName().equal(cookieName)){ String cookieValue = cookie.getValue(); } } ```

java代码
在CookieTest中存入Cookie,再到GetCookieInfo中获取

package com.torain.web;

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 CookieTest extends HttpServlet {
	/**
	 * 在本Servlet中持久化Cookie
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取Cookie对象
		Cookie cookie = new Cookie("goods","iPad");
		// 设置持久化时间
		cookie.setMaxAge(10*60);
		// 设置作用域
		cookie.setPath("/WEBTEST");
		// 发送Cookie
		response.addCookie(cookie);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
package com.torain.web;

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 GetCookieInfo extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取Cookie
		Cookie[] cookies = request.getCookies();
		// 遍历Cookie获取goods值
		for(Cookie cookie : cookies) {
			if (cookie.getName().equals("goods")) {
				response.getWriter().write(cookie.getValue());	
			}
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

Session

获取Session对象
HttpSession session = request.getSession();
此方法会获得专属于当前会话的Session对象,如果服务器端没有该会话的Session 对象会创建一个新的Session返回,如果已经有了属于该会话的Session直接将已有的Session返回(实质就是根据JSESSIONID判断该客户端是否在服务器上已经存在 session了)
session存取数据
session也是一个域对象使用域对象通用方法
session.setAttribute(String name,Object obj);
session.getAttribute(String name);
session.removeAttribute(String name);

Session对象的声明周期
创建

  • 第一次执行request.getSession()时创建

销毁

  • 服务器关闭时
  • Session过期(默认30分钟)
  • 手动销毁session.invalidate();

总结:session是保存在服务器上的,而会话关闭后服务器上的session域无人认领只能等待自动销毁,将JESSIONID借助Cookie持久化到客户端中,就可以再次使用session域

Session-java代码

package com.torain.web;

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;
import javax.servlet.http.HttpSession;

public class SessionServletA extends HttpServlet {
	/**
	 * 获取一个Session对象,存入值
	 * 将Session的JESSIONID持久化到客户端
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取Session对象
		HttpSession session = request.getSession();
		// 向Session域中存值
		session.setAttribute("name", "张三");
		// 将Session的JSESSIONID持久化到客户端
		String id = session.getId();
		Cookie cookie = new Cookie("JSESSIONID",id);
		// 持久化时间3分钟
		cookie.setMaxAge(180);
		// 发送Seesion
		response.addCookie(cookie);;
		// 打印
		response.getWriter().write("JSESSIONID:" + id);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

猜你喜欢

转载自www.cnblogs.com/torain/p/12897978.html
今日推荐