获取当前用户的名字

protected String getSid(HttpServletRequest request) {
		//获取到session
		HttpSession session = request.getSession();
		SID id = null;
		 String idStr = null;
		 //首先从session中获取id
		 id = (SID)session.getAttribute("sid");
		 //如果id不存在,则在cookie中获取
		 if(id == null){
		 //通过request获取cookie
		  Cookie[] cks = request.getCookies();
		  //循环cookie的内容,判断是否存在一个cookie的name为sid
		  for(int i = 0; i < cks.length; i++){
			  Cookie c = cks[i]; 
			  if("sid".equals(c.getName())){
				  try {
					id = new SID(URLDecoder.decode(c.getValue(),"utf-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			  }
		  }
		}
		  
		return id.getName();
	}


 

你可能感兴趣的:(获取当前用户的名字)