java设置cookie,存储和读取

	/**【【【【【【【【【【这里是存储的一种方式】】】】】】】】】】】】】
	 * 设置cookie:根据用户的选择设置默认树
	 * 将设置的axis的value存入cookie
	 */
	@RequestMapping(value="default_axis.jspx")
	public String setDefault_docTree(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		RequestProxy proxy = RequestProxy.fromRequest(request);
		//这里设置默认树
		Cookie defaultCookie = null;
		String defaultAxisId = proxy.getChineseParameter("defaultAxisId");
		if(!StringUtil.isEmpty(defaultAxisId)){
			TAdminuser user = (TAdminuser)session.getAttribute(Global.SESSION_USER_NAME);
			if(user != null){
				defaultCookie = new Cookie(user.getId().toString(), defaultAxisId);
				defaultCookie.setPath("/");
				defaultCookie.setMaxAge(365*24*24*60);
				response.addCookie(defaultCookie);
			}

		}
		return "redirect:doc_tree.jspx";
	}


cookie的name可以是动态的值!

defaultCookie = new Cookie(user.getId().toString(), defaultAxisId);

Cookie[] cookies = request.getCookies();   //【【【【【【【【【【读取】】】】】】】】】】】】】

		String oldValue = "";
		List<Hotel> cookiesList = new ArrayList<Hotel>();
		if(cookies != null){
			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().equalsIgnoreCase("hotel_name_id_cookie")) {  //注意用equalIgnoreCase 而不是 ==
					oldValue = cookies[i].getValue();
					try {
						oldValue = URLDecoder.decode(oldValue, "UTF-8");
						String[] arrs = oldValue.split(";");
						for (int j = 0; j < arrs.length; j++) {
							String _hotel_cd = arrs[j].substring(0,
									arrs[j].indexOf("*"));
							String _hotel_Name = arrs[j].substring(arrs[j]
									.indexOf("*") + 1);
							Hotel h = new Hotel();
							h.setS_HotelCd(_hotel_cd);
							h.setS_Hotel(URLDecoder.decode(_hotel_Name, "UTF-8"));
							cookiesList.add(h);  
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		request.setAttribute("cookiesList", cookiesList);


				String val = "";  //【【【【【【【【【【这里是存储的另一种方式】】】】】】】】】】】】】

				if(oldValue!=null && !oldValue.equals("")){
					val = oldValue + ";" + hotelInfo.getHotel().getS_HotelCd()+"*"+hotelInfo.getHotel().getS_Hotel();
					val = URLEncoder.encode(val,"UTF-8");
				}else{
					val = hotelInfo.getHotel().getS_HotelCd()+"*"+hotelInfo.getHotel().getS_Hotel();
					val = URLEncoder.encode(val,"UTF-8");
				}
				Cookie cookie = new Cookie("hotel_name_id_cookie", val);
				cookie.setPath("/");
				cookie.setMaxAge(365 * 24 * 60 * 60);
				response.addCookie(cookie);   //一定要add,不然读取的时候读不到或者都是null






							String val = "";
				if(oldValue!=null && !oldValue.equals("")){
					val = oldValue + ";" + hotelInfo.getHotel().getS_HotelCd()+"*"+hotelInfo.getHotel().getS_Hotel();
					val = URLEncoder.encode(val,"UTF-8");
				}else{
					val = hotelInfo.getHotel().getS_HotelCd()+"*"+hotelInfo.getHotel().getS_Hotel();
					val = URLEncoder.encode(val,"UTF-8");
				}
				Cookie cookie = new Cookie("hotel_name_id_cookie", val);
				cookie.setPath("/");
				cookie.setMaxAge(365 * 24 * 60 * 60);
				response.addCookie(cookie);   //一定要add,不然读取的时候读不到或者都是null


cookie的name可以是动态的值!cookie的name可以是动态的值!


你可能感兴趣的:(java读取cookie,java存储cookie)