Example usage for javax.servlet.http Cookie Cookie

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

Introduction

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

Prototype

public Cookie(String name, String value) 

Source Link

Document

Constructs a cookie with the specified name and value.

Usage

From source file:com.eryansky.common.web.utils.RequestUtil.java

public static void setCookie(HttpServletResponse response, String name, String value, String path) {
    if (logger.isDebugEnabled()) {
        logger.debug("Cookie '" + name + "',?: '" + path + "'");
    }//from   ww  w.j a va 2  s .co m

    Cookie cookie = new Cookie(name, value);
    cookie.setSecure(false);
    cookie.setPath(path);
    cookie.setMaxAge(2592000);

    response.addCookie(cookie);
}

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

/**
 *  Cookie/*from  www  .j  av a 2 s .  c o m*/
 * 
 * @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:AddCookieServlet.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String data = request.getParameter("data");
    Cookie cookie = new Cookie("MyCookie", data);
    response.addCookie(cookie);/* w w w. j a v a  2s. co m*/
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    pw.println("<B>MyCookie has been set to");
    pw.println(data);
    pw.close();
}

From source file:com.ieasy.basic.util.CookieSupport.java

/**
 * cookies//from  w ww .  j av  a  2 s . c o m
 * 
 * @param response
 * @param cookieParams
 * @param maxAge
 */
public static final void writeCookies(HttpServletResponse response, Map<String, String> cookieParams,
        int maxAge) {
    if (cookieParams == null || cookieParams.size() == 0)
        return;
    Set<String> keySet = cookieParams.keySet();
    for (String key : keySet) {
        Cookie cookie = new Cookie(key, cookieParams.get(key));
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
    }
}

From source file:com.kamike.misc.CookieUtils.java

/**
 *
 * COOKIE/*from   w  ww. j  av a 2  s .  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);

    cookie.setPath("/");

    response.addCookie(cookie);

}

From source file:modelo.AutenticacionManager.Autenticacion.java

private static void variablesSession(HttpSession sesion, HttpServletResponse response, String login, String rol,
        String ip) {//  ww w . ja  v  a  2 s  .  c  om

    //public static void variablesSession(HttpSession sesion, String login, String ip, String rol, String persona, String codigoPersona) {
    sesion.setAttribute("login", login);
    sesion.setAttribute("ip", ip);

    Cookie ssocookie = new Cookie("login", encode(login));
    ssocookie.setPath("/");
    response.addCookie(ssocookie);

    ssocookie = new Cookie("ip", encode(ip));
    ssocookie.setPath("/");
    response.addCookie(ssocookie);

    /*ssocookie = new Cookie("rol", encode(rol));
    ssocookie.setPath("/");
    response.addCookie(ssocookie);*/

}

From source file:ru.org.linux.csrf.CSRFProtectionService.java

public static void generateCSRFCookie(HttpServletRequest request, HttpServletResponse response) {
    SecureRandom random = new SecureRandom();

    byte[] value = new byte[16];
    random.nextBytes(value);//from w ww.  ja v a  2s  .com

    String token = new String(Base64.encodeBase64(value));

    Cookie cookie = new Cookie(CSRF_COOKIE, token);
    cookie.setMaxAge(TWO_YEARS);
    cookie.setPath("/");
    response.addCookie(cookie);

    request.setAttribute(CSRF_ATTRIBUTE, token);
}

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

/**
 * cookie//from ww  w .  j av a 2s.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.iterzp.momo.utils.WebUtils.java

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

From source file:MyServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.println("<HTML>");
    out.println("<HEAD>");
    out.println("<TITLE>");
    out.println("A Web Page");
    out.println("</TITLE>");
    out.println("</HEAD>");
    out.println("<BODY");

    Cookie[] cookies = request.getCookies();
    boolean foundCookie = false;

    for (int i = 0; i < cookies.length; i++) {
        Cookie cookie1 = cookies[i];//w  ww  . j  a v a2  s .c  o  m
        if (cookie1.getName().equals("color")) {
            out.println("bgcolor = " + cookie1.getValue());
            foundCookie = true;
        }
    }

    if (!foundCookie) {
        Cookie cookie1 = new Cookie("color", "cyan");
        cookie1.setMaxAge(24 * 60 * 60);
        response.addCookie(cookie1);
    }

    out.println(">");
    out.println("<H1>Setting and Reading Cookies</H1>");
    out.println("This page will set its background color using a cookie when reloaded.");
    out.println("</BODY>");
    out.println("</HTML>");
}