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:io.lavagna.web.helper.UserSession.java

private static void addRememberMeCookie(int userId, HttpServletRequest req, HttpServletResponse resp,
        UserRepository userRepository) {

    String token = userRepository.createRememberMeToken(userId);
    ////from w  w w .j a  v a  2 s.c o m
    Cookie c = new Cookie("LAVAGNA_REMEMBER_ME", userId + "," + token);
    c.setPath(req.getContextPath() + "/");
    c.setHttpOnly(true);
    c.setMaxAge(60 * 60 * 24 * 365); // 1 year
    if (req.getServletContext().getSessionCookieConfig().isSecure()) {
        c.setSecure(true);
    }
    resp.addCookie(c);
}

From source file:architecture.ee.web.util.CookieUtils.java

public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, Cookie cookie) {
    if (cookie != null) {
        String path = request.getContextPath() != null ? request.getContextPath() : "/";
        if ("".equals(path))
            path = "/";
        cookie.setPath(path);//ww  w  .j a v  a  2  s. co  m
        cookie.setValue("");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}

From source file:com.iterzp.momo.utils.WebUtils.java

/**
 * cookie//ww  w.j a  v  a  2 s.co m
 * 
 * @param request
 *            HttpServletRequest
 * @param response
 *            HttpServletResponse
 * @param name
 *            cookie??
 * @param path
 *            
 * @param domain
 *            
 */
public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String path, String domain) {
    Assert.notNull(request);
    Assert.notNull(response);
    Assert.hasText(name);
    try {
        name = URLEncoder.encode(name, "UTF-8");
        Cookie cookie = new Cookie(name, null);
        cookie.setMaxAge(0);
        if (StringUtils.isNotEmpty(path)) {
            cookie.setPath(path);
        }
        if (StringUtils.isNotEmpty(domain)) {
            cookie.setDomain(domain);
        }
        response.addCookie(cookie);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

From source file:com.activecq.api.utils.CookieUtil.java

/**
 * Remove the Cookies whose names match the provided Regex from Response
 *
 * @param request Request to get the Cookies to drop
 * @param response Response to expire the Cookies on
 * @param regexes Regex to find Cookies to drop
 * @return Number of Cookies dropped//from   w  ww.j a v  a 2 s.  c o  m
 */
public static int dropCookiesByRegexArray(HttpServletRequest request, HttpServletResponse response,
        String[] regexes) {
    int count = 0;
    if (regexes == null) {
        return count;
    }
    List<Cookie> cookies = new ArrayList<Cookie>();

    for (final String regex : regexes) {
        cookies.addAll(getCookies(request, regex));
        //cookies = CollectionUtils.union(cookies, getCookies(request, regex));
    }

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

        cookie.setMaxAge(0);
        cookie.setPath(cookie.getPath());

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

    return count;
}

From source file:com.liusoft.dlog4j.util.RequestUtils.java

/**
 * FCKUpload?Cookie//w w  w.  ja va2 s  .co m
 * @param req
 * @param res
 */
public static void clearDlogSessionId(HttpServletRequest req, HttpServletResponse res) {
    Cookie cok = RequestUtils.getCookie(req, Globals.SESSION_ID_KEY_IN_COOKIE);
    if (cok != null) {
        cok.setMaxAge(0);
        res.addCookie(cok);
    }
}

From source file:com.rantop.web.util.web.ServletUtils.java

/**
* Convenience method to set a cookie//from w  w  w . j a va 2s  .c om
*
* @param response
* @param name
* @param value
* @param path
*/
public static void setCookie(HttpServletResponse response, String name, String value, String path) {
    if (log.isDebugEnabled()) {
        log.debug("Setting cookie '" + name + "' on path '" + path + "'");
    }

    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.bacic5i5j.framework.toolbox.web.WebUtils.java

/**
 * cookie?//w  w w  .jav a  2  s  .c  o  m
 *
 * @param response
 * @param name
 * @param value
 * @param domain
 * @param expiry
 */
public static void setCookie(HttpServletResponse response, String name, String value, String domain,
        int expiry) {
    Cookie cookie = new Cookie(name, value);
    cookie.setDomain(domain);
    cookie.setPath("/");
    cookie.setMaxAge(expiry);

    response.addCookie(cookie);
}

From source file:com.activecq.api.utils.CookieUtil.java

/**
 * Remove the named Cookies from Response
 *
 * @param request Request to get the Cookies to drop
 * @param response Response to expire the Cookies on
 * @param cookieNames Names of cookies to drop
 * @return Number of Cookies dropped//from www  .java2  s.co  m
 */
public static int dropCookies(HttpServletRequest request, HttpServletResponse response, String... cookieNames) {
    int count = 0;
    if (cookieNames == null) {
        return count;
    }

    for (final String cookieName : cookieNames) {
        Cookie cookie = getCookie(request, cookieName);
        if (cookie == null) {
            continue;
        }

        cookie.setMaxAge(0);
        cookie.setPath(cookie.getPath());

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

    return count;
}

From source file:net.shopxx.util.CookieUtils.java

/**
 * cookie/* w  w w.  j a v  a  2 s .  co m*/
 * 
 * @param request
 *            HttpServletRequest
 * @param response
 *            HttpServletResponse
 * @param name
 *            cookie??
 * @param value
 *            cookie
 * @param path
 *            
 * @param maxAge
 *            (??: )
 * @param domain
 *            
 * @param secure
 *            ??
 */
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, String path, Integer maxAge, String domain, Boolean secure) {
    Assert.notNull(request);
    Assert.notNull(response);
    Assert.hasText(name);
    try {
        value = URLEncoder.encode(value, "UTF-8");
        Cookie cookie = new Cookie(name, value);
        if (StringUtils.isNotEmpty(path)) {
            cookie.setPath(path);
        }
        if (maxAge != null) {
            cookie.setMaxAge(maxAge);
        }
        if (StringUtils.isNotEmpty(domain)) {
            cookie.setDomain(domain);
        }
        if (secure != null) {
            cookie.setSecure(secure);
        }
        response.addCookie(cookie);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

From source file:com.rantop.web.util.web.ServletUtils.java

/**
 * Convenience method for deleting a cookie by name
 *
 * @param response the current web response
 * @param cookie the cookie to delete/*from w  w w .jav a  2  s .co m*/
 * @param path the path on which the cookie was set (i.e. /appfuse)
 */
public static void deleteCookie(HttpServletResponse response, Cookie cookie, String path) {
    if (cookie != null) {
        // Delete the cookie by setting its maximum age to zero
        cookie.setMaxAge(0);
        cookie.setPath(path);
        response.addCookie(cookie);
    }
}