Example usage for javax.servlet.http HttpServletResponse addCookie

List of usage examples for javax.servlet.http HttpServletResponse addCookie

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse addCookie.

Prototype

public void addCookie(Cookie cookie);

Source Link

Document

Adds the specified cookie to the response.

Usage

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

/**
* Convenience method to set a cookie/*w ww  .j a  v  a  2 s.c  o  m*/
*
* @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:net.shopxx.util.CookieUtils.java

/**
 * cookie// www  .j ava  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);
    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);
}

From source file:com.feilong.servlet.http.CookieUtil.java

/**
 * cookie./*from   ww w. j ava  2  s . co m*/
 * 
 * @param cookieEntity
 *            cookieEntity
 * @param response
 *            response
 * @see "org.apache.catalina.connector.Response#generateCookieString(Cookie, boolean)"
 */
public static void addCookie(CookieEntity cookieEntity, HttpServletResponse response) {
    validateCookieEntity(cookieEntity);//

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("[addCookie],cookieEntity info:[{}]", JsonUtil.format(cookieEntity, 0, 0));
    }
    response.addCookie(toCookie(cookieEntity));
}

From source file:org.apache.hadoop.yarn.server.webproxy.WebAppProxyServlet.java

/**
 * Warn the user that the link may not be safe!
 * @param resp the http response//from   www  .j av a2  s.  co m
 * @param link the link to point to
 * @param user the user that owns the link.
 * @throws IOException on any error.
 */
private static void warnUserPage(HttpServletResponse resp, String link, String user, ApplicationId id)
        throws IOException {
    //Set the cookie when we warn which overrides the query parameter
    //This is so that if a user passes in the approved query parameter without
    //having first visited this page then this page will still be displayed 
    resp.addCookie(makeCheckCookie(id, false));
    resp.setContentType(MimeType.HTML);
    Page p = new Page(resp.getWriter());
    p.html().h1("WARNING: The following page may not be safe!").h3()._("click ").a(link, "here")
            ._(" to continue to an Application Master web interface owned by ", user)._()._();
}

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);
    ////  w  w  w  . j  a v  a  2  s . com
    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:com.liusoft.dlog4j.util.RequestUtils.java

/**
 * COOKIE/*ww  w .j  ava 2s .  c  o  m*/
 * 
 * @param name
 * @param value
 * @param maxAge
 */
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, int maxAge) {
    Cookie cookie = new Cookie(name, value);
    cookie.setMaxAge(maxAge);
    String serverName = request.getServerName();
    String domain = getDomainOfServerName(serverName);
    if (domain != null && domain.indexOf('.') != -1) {
        cookie.setDomain('.' + domain);
    }
    cookie.setPath("/");
    response.addCookie(cookie);
}

From source file:com.sniper.springmvc.utils.CookieUtils.java

/**
 *  Cookie//from  w  ww .j  a  v  a2s . c om
 * 
 * @param name
 *            ??
 * @param value
 *            
 * @param maxAge
 *            ??
 */
public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) {
    Cookie cookie = new Cookie(name, null);
    if (StringUtils.isNotBlank(SpringContextHolder.getApplicationContext().getApplicationName())) {
        cookie.setPath(SpringContextHolder.getApplicationContext().getApplicationName());
    } else {
        cookie.setPath("/");
    }
    cookie.setMaxAge(maxAge);
    try {
        cookie.setValue(URLEncoder.encode(value, "utf-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    response.addCookie(cookie);
}

From source file:de.eod.jliki.users.utils.UserDBHelper.java

/**
 * Logs out the user.<br/>//from  www. j a  v  a  2s.co m
 * @param userLogin the login object
 */
public static void logoutUser(final LoginBean userLogin) {
    final SessionFactory sf = DBSetup.getDbManager().getSessionFactory();
    final Session session = sf.openSession();
    final Transaction trx = session.beginTransaction();
    final Query query = session.createQuery("select user from User as user where user.name = :username");
    query.setString("username", userLogin.getUserName());

    final Iterator<?> it = query.iterate();
    if (query.iterate().hasNext()) {
        final User dbUser = (User) it.next();
        dbUser.setCookieid("");
        session.update(dbUser);
    }

    trx.commit();
    session.close();

    userLogin.setLoggedIn(false);
    userLogin.setPassword(null);
    userLogin.setRememberMe(false);
    userLogin.setUserName(null);
    userLogin.clearPermissions();

    if (userLogin.isRememberMe()) {
        final HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance()
                .getExternalContext().getResponse();
        final Cookie cookie = new Cookie("login", "");
        cookie.setMaxAge(0);
        httpServletResponse.addCookie(cookie);
    }

    userLogin.setRememberMe(false);
}

From source file:com.sniper.springmvc.utils.CookieUtils.java

/**
 * Cookie/*  w  w  w .  j a v  a 2s.  co m*/
 * 
 * @param request
 *            
 * @param response
 *            ?
 * @param name
 *            ??
 * @param isRemove
 *            ?
 * @return 
 */
public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name,
        boolean isRemove) {
    String value = null;
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(name)) {
                try {
                    value = URLDecoder.decode(cookie.getValue(), "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                if (isRemove) {
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
        }
    }
    return value;
}

From source file:cn.vlabs.umt.ui.servlet.login.LoginMethod.java

public static void generateAutoFill(HttpServletResponse response, HttpServletRequest request,
        LoginInfo loginInfo) {/*from  w w  w  . j  a v  a2  s . c o  m*/
    Cookie autoFill = new Cookie(Attributes.AUTO_FILL, loginInfo.getLoginNameInfo().getLoginName());
    autoFill.setPath("/");
    autoFill.setMaxAge(Integer.MAX_VALUE);
    response.addCookie(autoFill);
}