Session与Cookie初探(一)

本文详细介绍了Session的核心方法及其使用,包括设置、获取、删除属性等操作,并通过实例展示了如何利用Session进行客户端信息管理,如判断客户端是否为新会话、设置会话超时时间等。
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.youkuaiyun.com/qingfeng812/article/details/51955210

Session 核心方法

public void setAttribute(String name,Object value)

将value对象以name名称绑定到会话

public object getAttribute(String name)

取得name的属性值,如果属性不存在则返回null

public void removeAttribute(String name)

从会话中删除name属性,如果不存在不会执行,也不会抛处错误.

public Enumeration getAttributeNames()

返回和会话有关的枚举值

public void invalidate()

使会话失效,同时删除属性对象

public Boolean isNew()

用于检测当前客户是否为新的会话

public long getCreationTime()

返回会话创建时间

public long getLastAccessedTime()

返回在会话时间内web容器接收到客户最后发出的请求的时间

public int getMaxInactiveInterval()

返回在会话期间内客户请求的最长时间.秒

public void setMaxInactiveInterval(int seconds)

允许客户客户请求的最长时间

ServletContext getServletContext()

返回当前会话的上下文环境,ServletContext对象可以使Servlet与web容器进行通信

public String getId()

返回会话期间的识别号

 
   项目代码见:
  https://github.com/Arisono/SpringAppServer
 
  要运行程序:
  需要掌握:maven+spring mvc+eclipse+git
  
  运行效果:

  

服务器程序:
	@RequestMapping(value = "/client/info")
	public @ResponseBody Map<String, Object> getClientBaseInfo(
			HttpServletRequest request,
			HttpServletResponse response,
			@CookieValue(value = "JSESSIONID", required = false) String sessionId) {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> resultBody = new HashMap<String, Object>();
		String ipAddress = getIPAddress(request);// 获取客户端ip地址
		Enumeration headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String key = (String) headerNames.nextElement();
			String value = request.getHeader(key);
			result.put(key, value);
		}
		request.getHeader("cache-control");

		boolean isMobile = HttpRequestDeviceUtils.isMobileDevice(request);
		resultBody.put("isMoblie", isMobile);
		resultBody.put("headers", result);
		resultBody.put("JSESSIONID", sessionId);
		resultBody.put("ip", ipAddress);
		HttpSession httpSession = request.getSession();
		httpSession.setMaxInactiveInterval(60);
		resultBody.put("sessionId", httpSession.getAttribute("sessionId"));
		if (httpSession != null
				&& !StringUtils.isEmpty(httpSession.getAttribute("sessionId"))) {
			if (httpSession.getAttribute("sessionId").equals(sessionId)) {
				// 登陆保持状态
				resultBody.put("loginState", "登陆状态!在线!");

			} else {
				// 登录断开状态
				resultBody.put("loginState", "会话断开!");
				httpSession.setAttribute("sessionId", httpSession.getId());
			}
		} else {
			// 登录断开状态
			resultBody.put("loginState", "会话断开!");
			httpSession.setAttribute("sessionId", httpSession.getId());
		}

		resultBody.put("MaxInactiveInterval",
				httpSession.getMaxInactiveInterval());
		resultBody
				.put("LastAccessedTime", DateFormatUtil
						.getFormatDate(httpSession.getLastAccessedTime()));
		resultBody.put("CreationTime",
				DateFormatUtil.getFormatDate(httpSession.getCreationTime()));
		resultBody.put("isNew", httpSession.isNew());
		resultBody.put("HttpSessionId", httpSession.getId());

		return resultBody;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值