javax.servlet.http.Cookie.<init>()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(167)

本文整理了Java中javax.servlet.http.Cookie.<init>()方法的一些代码示例,展示了Cookie.<init>()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cookie.<init>()方法的具体详情如下:
包路径:javax.servlet.http.Cookie
类名称:Cookie
方法名:<init>

Cookie.<init>介绍

[英]Constructs a cookie with the specified name and value.

The name must conform to RFC 2109. However, vendors may provide a configuration option that allows cookie names conforming to the original Netscape Cookie Specification to be accepted.

The name of a cookie cannot be changed once the cookie has been created.

The value can be anything the server chooses to send. Its value is probably of interest only to the server. The cookie's value can be changed after creation with the setValue method.

By default, cookies are created according to the Netscape cookie specification. The version can be changed with the setVersion method.
[中]使用指定的名称和值构造cookie。
名称必须符合RFC 2109。但是,供应商可能会提供一个配置选项,允许接受符合原始Netscape cookie规范的cookie名称。
创建cookie后,无法更改cookie的名称。
该值可以是服务器选择发送的任何内容。它的值可能只对服务器感兴趣。cookie的值可以在使用setValue方法创建后更改。
默认情况下,cookie是根据Netscape cookie规范创建的。可以使用setVersion方法更改版本。

代码示例

代码示例来源:origin: perwendel/spark

/**
 * Removes the cookie with given path and name.
 *
 * @param path path of the cookie
 * @param name name of the cookie
 */
public void removeCookie(String path, String name) {
  Cookie cookie = new Cookie(name, "");
  cookie.setPath(path);
  cookie.setMaxAge(0);
  response.addCookie(cookie);
}

代码示例来源:origin: looly/hutool

/**
 * 设定返回给客户端的Cookie
 * 
 * @param response 响应对象{@link HttpServletResponse}
 * @param name Cookie名
 * @param value Cookie值
 */
public final static void addCookie(HttpServletResponse response, String name, String value) {
  response.addCookie(new Cookie(name, value));
}

代码示例来源:origin: azkaban/azkaban

/**
 * Sets a message in azkaban.success.message in the cookie. This will be used by the web client
 * javascript to somehow display the message
 */
protected void setSuccessMessageInCookie(final HttpServletResponse response,
  final String message) {
 final Cookie cookie = new Cookie(AZKABAN_SUCCESS_MESSAGE, message);
 cookie.setPath("/");
 response.addCookie(cookie);
}

代码示例来源:origin: spring-projects/spring-framework

@DeleteMapping(path = "/", produces = "text/plain")
  String deleteCookie(HttpServletResponse response) {
    javax.servlet.http.Cookie cookie = new javax.servlet.http.Cookie(COOKIE_NAME, "");
    cookie.setMaxAge(0);
    response.addCookie(cookie);
    return "Delete";
  }
}

代码示例来源:origin: pippo-java/pippo

/**
 * Generates a language cookie with a very long max age (ten years).
 *
 * @param language
 * @return The cookie
 */
private Cookie generateLanguageCookie(String language) {
  Cookie cookie = new Cookie(applicationCookiePrefix + "_LANG", language);
  cookie.setSecure(true);
  cookie.setMaxAge(TEN_YEARS);
  return cookie;
}

代码示例来源:origin: cloudfoundry/uaa

public Cookie getNullCookie() {
  Cookie currentUserCookie = new Cookie(CURRENT_USER_COOKIE_NAME, null);
  currentUserCookie.setHttpOnly(false);
  currentUserCookie.setMaxAge(0);
  currentUserCookie.setPath("/");
  return currentUserCookie;
}

代码示例来源:origin: spring-projects/spring-restdocs

@RequestMapping("/set-cookie")
public void setCookie(HttpServletResponse response) {
  Cookie cookie = new Cookie("name", "value");
  cookie.setDomain("localhost");
  cookie.setHttpOnly(true);
  response.addCookie(cookie);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Create a cookie with the given value, using the cookie descriptor
 * settings of this generator (except for "cookieMaxAge").
 * @param cookieValue the value of the cookie to crate
 * @return the cookie
 * @see #setCookieName
 * @see #setCookieDomain
 * @see #setCookiePath
 */
protected Cookie createCookie(String cookieValue) {
  Cookie cookie = new Cookie(getCookieName(), cookieValue);
  if (getCookieDomain() != null) {
    cookie.setDomain(getCookieDomain());
  }
  cookie.setPath(getCookiePath());
  return cookie;
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Changes the icon size by changing the cookie
 */
public void doIconSize( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
  String qs = req.getQueryString();
  if(qs==null)
    throw new ServletException();
  Cookie cookie = new Cookie("iconSize", Functions.validateIconSize(qs));
  cookie.setMaxAge(/* ~4 mo. */9999999); // #762
  rsp.addCookie(cookie);
  String ref = req.getHeader("Referer");
  if(ref==null)   ref=".";
  rsp.sendRedirect2(ref);
}

代码示例来源:origin: pippo-java/pippo

private Cookie createSessionIdCookie(HttpServletRequest request, String sessionId) {
  Cookie cookie = new Cookie(SESSION_ID_COOKIE_NAME, sessionId);
  cookie.setHttpOnly(true);
  cookie.setSecure(request.isSecure());
  cookie.setPath(request.getContextPath() + "/");
  // TODO setDomain
  return cookie;
}

代码示例来源:origin: pippo-java/pippo

/**
 * Adds a cookie to the response.
 *
 * @param name
 * @param value
 * @return the response
 */
public Response cookie(String name, String value) {
  Cookie cookie = new Cookie(name, value);
  cookie.setSecure(true);
  addCookie(cookie);
  return this;
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void responseWithCookie() {
  MockHttpServletResponse response = new MockHttpServletResponse();
  response.setStatus(HttpServletResponse.SC_OK);
  Cookie cookie = new Cookie("name", "value");
  cookie.setDomain("localhost");
  cookie.setHttpOnly(true);
  response.addCookie(cookie);
  OperationResponse operationResponse = this.factory.convert(response);
  assertThat(operationResponse.getHeaders()).hasSize(1);
  assertThat(operationResponse.getHeaders()).containsEntry(HttpHeaders.SET_COOKIE,
      Collections.singletonList("name=value; Domain=localhost; HttpOnly"));
}

代码示例来源:origin: shuzheng/zheng

/**
 * 设置cookie
 * @param response
 * @param name
 * @param value
 * @param maxAge
 */
public static void setCookie(HttpServletResponse response, String name, String value, String path, int maxAge) {
  Cookie cookie = new Cookie(name, value);
  cookie.setPath(path);
  if (maxAge > 0) {
    cookie.setMaxAge(maxAge);
  }
  response.addCookie(cookie);
}
public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) {

代码示例来源:origin: azkaban/azkaban

/**
 * Sets an error message in azkaban.failure.message in the cookie. This will be used by the web
 * client javascript to somehow display the message
 */
protected void setErrorMessageInCookie(final HttpServletResponse response,
  final String errorMsg) {
 final Cookie cookie = new Cookie(AZKABAN_FAILURE_MESSAGE, errorMsg);
 cookie.setPath("/");
 response.addCookie(cookie);
}

代码示例来源:origin: looly/hutool

/**
 * 设定返回给客户端的Cookie
 * 
 * @param response 响应对象{@link HttpServletResponse}
 * @param name Cookie名
 * @param value Cookie值
 */
public final static void addCookie(HttpServletResponse response, String name, String value) {
  response.addCookie(new Cookie(name, value));
}

代码示例来源:origin: ZHENFENG13/My-Blog

/**
 * 退出登录状态
 *
 * @param session
 * @param response
 */
public static void logout(HttpSession session, HttpServletResponse response) {
  session.removeAttribute(WebConst.LOGIN_SESSION_KEY);
  Cookie cookie = new Cookie(WebConst.USER_IN_COOKIE, "");
  cookie.setMaxAge(0);
  response.addCookie(cookie);
  try {
    response.sendRedirect(Commons.site_url());
  } catch (IOException e) {
    LOGGER.error(e.getMessage(), e);
  }
}

代码示例来源:origin: pippo-java/pippo

/**
 * Removes the specified cookie by name.
 *
 * @param name
 * @return the response
 */
public Response removeCookie(String name) {
  Cookie cookie = new Cookie(name, "");
  cookie.setSecure(true);
  cookie.setMaxAge(0);
  addCookie(cookie);
  return this;
}

代码示例来源:origin: cloudfoundry/uaa

public Cookie getCookie(UaaPrincipal uaaPrincipal) throws CurrentUserCookieEncodingException {
  CurrentUserInformation currentUserInformation = new CurrentUserInformation();
  currentUserInformation.setUserId(uaaPrincipal.getId());
  Cookie cookie = new Cookie(CURRENT_USER_COOKIE_NAME, urlEncode(JsonUtils.writeValueAsString(currentUserInformation)));
  cookie.setPath("/");
  cookie.setHttpOnly(false);
  cookie.setMaxAge(sessionTimeout);
  return cookie;
}

代码示例来源:origin: spring-projects/spring-restdocs

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
  Cookie cookie = new Cookie("name", "value");
  cookie.setDomain("localhost");
  cookie.setHttpOnly(true);
  resp.addCookie(cookie);
}

代码示例来源:origin: org.springframework/spring-web

/**
 * Create a cookie with the given value, using the cookie descriptor
 * settings of this generator (except for "cookieMaxAge").
 * @param cookieValue the value of the cookie to crate
 * @return the cookie
 * @see #setCookieName
 * @see #setCookieDomain
 * @see #setCookiePath
 */
protected Cookie createCookie(String cookieValue) {
  Cookie cookie = new Cookie(getCookieName(), cookieValue);
  if (getCookieDomain() != null) {
    cookie.setDomain(getCookieDomain());
  }
  cookie.setPath(getCookiePath());
  return cookie;
}

相关文章