CookieUtil.java
上传用户:zghglow
上传日期:2022-08-09
资源大小:27227k
文件大小:1k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

JavaScript

  1. package com.chinacannel.common;
  2. import javax.servlet.http.Cookie;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. public class CookieUtil {
  6.     public void setCookieValue(HttpServletResponse hsr, String cookiName,
  7.                                String cookiValue, int maxAge) {
  8.         Cookie cookie = new Cookie(cookiName, cookiValue);
  9.         cookie.setMaxAge(maxAge);
  10.         hsr.addCookie(cookie);
  11.     }
  12.     public String getCookieValueByName(HttpServletRequest hsr, String cookiName) {
  13.         Cookie[] cookies = hsr.getCookies();
  14.         for (int i = 0; i < cookies.length; i++) {
  15.             Cookie cookie = cookies[i];
  16.             if (cookiName.equals(cookie.getName())) {
  17.                 return cookie.getValue();
  18.             }
  19.         }
  20.         return null;
  21.     }
  22. }