且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Session与Cookie初探(一)

更新时间:2022-04-19 10:05:09

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/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对象可以使Servletweb容器进行通信

public String getId()

返回会话期间的识别号

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

  Session与Cookie初探(一)

服务器程序:
	@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;
	}