Example usage for javax.servlet.http Cookie setMaxAge

List of usage examples for javax.servlet.http Cookie setMaxAge

Introduction

In this page you can find the example usage for javax.servlet.http Cookie setMaxAge.

Prototype

public void setMaxAge(int expiry) 

Source Link

Document

Sets the maximum age in seconds for this Cookie.

Usage

From source file:com.xidu.framework.common.util.CookieUtils.java

/**
 * set the name/value entry to the cookie
 * // ww w . ja v  a  2  s . c o  m
 * @Date : 2011-3-23
 * @param response
 *            - HttpServletResponse's instance
 * @param name
 *            - Cookie's Entry key
 * @param value
 *            - Cookie's Entry value
 * @param path
 *            - Cookie's path
 * @param domain
 *            - Cookie' domain
 * @param maxAge
 *            - Cookie's max age
 */
public static void setCookie(HttpServletResponse response, String name, String value, String path,
        String domain, int maxAge) {
    logger.debug("cookie value:" + value);
    Cookie cookie = new Cookie(name, value);
    cookie.setSecure(false);
    if (StringUtils.isNotBlank(path)) {
        cookie.setPath(path);
    }
    cookie.setMaxAge(maxAge);
    if (StringUtils.isNotBlank(domain)) {
        cookie.setDomain(domain);
    }
    response.addCookie(cookie);
}

From source file:com.adobe.acs.commons.util.CookieUtil.java

/**
 * Internal method used for dropping cookies
 *
 * @param response/*w  ww .  j a  v  a  2  s  .  co  m*/
 * @param cookies
 * @param cookiePath
 * @return
 */
private static int dropCookies(final HttpServletResponse response, final Cookie[] cookies,
        final String cookiePath) {
    int count = 0;

    for (final Cookie cookie : cookies) {
        if (cookie == null) {
            continue;
        }

        final Cookie responseCookie = (Cookie) cookie.clone();
        responseCookie.setMaxAge(0);
        responseCookie.setPath(cookiePath);
        responseCookie.setValue("");

        addCookie(responseCookie, response);
        count++;
    }

    return count;
}

From source file:org.b3log.latke.util.Sessions.java

/**
 * Logins the specified user from the specified request.
 * //  w w  w.  j av a 2  s  . c o m
 * <p>
 * If no session of the specified request, do nothing.
 * </p>
 *
 * @param request the specified request
 * @param response the specified response
 * @param user the specified user, for example,
 * <pre>
 * {
 *     "userEmail": "",
 *     "userPassword": ""
 * }
 * </pre>
 */
public static void login(final HttpServletRequest request, final HttpServletResponse response,
        final JSONObject user) {
    final HttpSession session = request.getSession(false);

    if (null == session) {
        LOGGER.warning("The session is null");
        return;
    }

    session.setAttribute(User.USER, user);

    try {
        final JSONObject cookieJSONObject = new JSONObject();

        cookieJSONObject.put(User.USER_EMAIL, user.optString(User.USER_EMAIL));
        cookieJSONObject.put(User.USER_PASSWORD, user.optString(User.USER_PASSWORD));

        final Cookie cookie = new Cookie("b3log-latke", cookieJSONObject.toString());

        cookie.setPath("/");
        cookie.setMaxAge(COOKIE_EXPIRY);
        response.addCookie(cookie);
    } catch (final Exception e) {
        LOGGER.log(Level.WARNING, "Can not write cookie", e);
    }
}

From source file:edu.utah.further.i2b2.hook.further.web.ServletUtil.java

/**
 * Convenience method to set a cookie. The cookie gets max age set to 30 days.
 *
 * @param response//w w  w  .  j  a v a  2 s  .co  m
 *            response that will accept a cookie
 * @param name
 *            name of the cookie to store
 * @param value
 *            value of the cookie
 * @param path
 *            path of the cookie
 */
public static void setCookie(final HttpServletResponse response, final String name, final String value,
        final String path) {
    if (log.isDebugEnabled()) {
        log.debug("Setting cookie " + quote(name) + " on path " + quote(path));
    }

    final Cookie cookie = new Cookie(name, value);
    cookie.setSecure(false);
    cookie.setPath(path);
    cookie.setMaxAge(3600 * 24 * 30); // 30 days

    response.addCookie(cookie);
}

From source file:com.liferay.portal.util.CookieKeys.java

public static void addSupportCookie(HttpServletRequest request, HttpServletResponse response) {

    Cookie cookieSupportCookie = new Cookie(COOKIE_SUPPORT, "true");

    cookieSupportCookie.setPath(StringPool.SLASH);
    cookieSupportCookie.setMaxAge(MAX_AGE);

    addCookie(request, response, cookieSupportCookie);
}

From source file:com.ax.utils.CookieUtils.java

/**
 * Stores a value in a cookie. This cookie will persist for the amount
 * specified in the <tt>saveTime</tt> parameter.
 * /*from w w w  .j a  v a 2  s  . co  m*/
 * @see #setCookie(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,String,String)
 * @param request
 *            the servlet request.
 * @param response
 *            the servlet response.
 * @param name
 *            a name to identify the cookie.
 * @param value
 *            the value to store in the cookie.
 * @param maxAge
 *            the time (in seconds) this cookie should live.
 * @param domain
 *            the domain.
 * @param path
 *            the path.
 */
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, int maxAge, String domain, String path) {
    // Check to make sure the new value is not null (appservers like Tomcat
    // 4 blow up if the value is null).
    if (value == null) {
        value = "";
    }
    if (StringUtils.isEmpty(path)) {
        path = "/";
    }
    Cookie cookie = new Cookie(name, value);
    // maxAge0cookiemaxAge
    // if (maxAge > 0)
    // {
    cookie.setMaxAge(maxAge);
    // }
    cookie.setPath(path);
    // domain?cookiedomain
    if (!StringUtils.isEmpty(domain)) {
        cookie.setDomain(domain);
    }
    response.addCookie(cookie);
}

From source file:com.anjz.util.CookieUtils.java

private static void setCookie(String key, String value, int maxAge, String path, String domainName,
        final boolean httpOnly, final boolean secure, HttpServletResponse response) {
    if (response != null) {
        Cookie cookie = new Cookie(key, value);
        cookie.setMaxAge(maxAge);
        if (StringUtils.isNotBlank(path)) {
            cookie.setPath(path);/*from  w  ww  .  j  av  a  2s  .c  o m*/
        } else {
            cookie.setPath(PATH);
        }
        if (StringUtils.isNotBlank(domainName)) {
            cookie.setDomain(domainName);
        }
        cookie.setVersion(0);
        cookie.setSecure(secure);
        if (httpOnly) {
            final StringBuffer buf = new StringBuffer();
            getCookieHeaderValue(cookie, buf, httpOnly);
            response.addHeader(getCookieHeaderName(cookie), buf.toString());
        } else {
            response.addCookie(cookie);
        }
    }
}

From source file:cn.vlabs.duckling.vwb.VWBFilter.java

public static void removeGlobalCookie(HttpServletRequest request, HttpServletResponse response,
        HttpSession session) {//from  w  w w . j av a2 s .  c  o  m
    Cookie oldCookie = new Cookie(COOKIE_NAME, session.getId());
    oldCookie.setPath(request.getContextPath());
    oldCookie.setMaxAge(0);
    response.addCookie(oldCookie);
}

From source file:io.lavagna.web.helper.UserSession.java

public static void invalidate(HttpServletRequest req, HttpServletResponse resp, UserRepository userRepository) {
    req.getSession().invalidate();//w ww.  j av  a  2  s  .  c om
    Cookie c = getCookie(req, "LAVAGNA_REMEMBER_ME");
    if (c != null) {
        deleteTokenIfExist(c.getValue(), userRepository);
        c.setMaxAge(0);
        c.setValue(null);
        resp.addCookie(c);
    }
}

From source file:com.kingcore.framework.util.CookieUtils.java

/** 
 * Creates a Cookie with the specified name, value and max age,
 * and adds it to the response.// w  w w . j a  v a 2 s  .  c o m
 * cookies  cookie?Base64 ?
 *    The form of the domain name is specified by RFC 2109. A domain name begins with a dot (.foo.com) 
 *       and means that the cookie is visible to servers in a specified Domain Name System (DNS) zone 
 *       (for example, www.foo.com, but not a.b.foo.com). By default, cookies are only returned to 
 *       the server that sent them.
 * @param name cookie's name.
 * @param value cookie's value.
 * @param maxAge the time cookie been keeped. the unit is second.
 *      age of the cookie in seconds,??Cookie.
 *      an integer specifying the maximum age of the cookie in seconds; 
 *      if negative, means the cookie is not stored; if zero, deletes the cookie.
 * @param res response Object.
 * @param needEncode ??Cookie?Base64?true
 * @param domain Cookie's domain
 */
public static void sendCookie(String name, String value, int maxAge, HttpServletResponse response,
        boolean needEncode, String domain) {

    try {
        if (needEncode) {
            value = Base64.encode(value.getBytes("utf-8")); //?
            //              value = new String(Base64.encode( value.getBytes("utf-8")), "utf-8" );   //utf-8
        }
        //System.out.println("value = " + value);
        Cookie cookie = new Cookie(name, value);//Hex.encode(value.getBytes()) );
        cookie.setMaxAge(maxAge);
        cookie.setPath("/");
        if (domain != null) {
            cookie.setDomain(domain); // domain
        }
        response.addCookie(cookie);

    } catch (UnsupportedEncodingException e) {
        log.debug("debug", e);
        /// e.pri ntStackTrace();
    }

}