java設定cookie,儲存和讀取__java儲存cookie

來源:互聯網
上載者:User
/**【【【【【【【【【【這裡是儲存的一種方式】】】】】】】】】】】】】 * 設定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可以是動態值。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.