当前位置: 首页>>代码示例>>Java>>正文


Java Cookie.setDomain方法代码示例

本文整理汇总了Java中javax.servlet.http.Cookie.setDomain方法的典型用法代码示例。如果您正苦于以下问题:Java Cookie.setDomain方法的具体用法?Java Cookie.setDomain怎么用?Java Cookie.setDomain使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.servlet.http.Cookie的用法示例。


在下文中一共展示了Cookie.setDomain方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: echoCookies

import javax.servlet.http.Cookie; //导入方法依赖的package包/类
@RequestMapping(value = "/cookies", method = {GET, POST})
public List<Cookie> echoCookies(HttpServletRequest request, HttpServletResponse response) {
    Cookie[] cookies = request.getCookies();
    if (cookies == null) {
        return Collections.emptyList();
    } else {
        String domain = request.getParameter("domain");
        for (Cookie cookie: cookies) {
            if (domain != null) {
                cookie.setDomain(domain);
            }
            response.addCookie(cookie);
        }
        return Arrays.asList(cookies);
    }
}
 
开发者ID:intuit,项目名称:karate,代码行数:17,代码来源:SearchController.java

示例2: deleteCookie

import javax.servlet.http.Cookie; //导入方法依赖的package包/类
public static void deleteCookie(HttpServletRequest request, HttpServletResponse response,
        Cookie cookie, String domain, String path) {
    if (cookie != null) {
        if(StringUtils.isNotBlank(domain)){
            cookie.setDomain(domain);
        }
        cookie.setPath(path);
        cookie.setValue("");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}
 
开发者ID:bridgeli,项目名称:netty-socketio-demo,代码行数:13,代码来源:CookieUtils.java

示例3: removeCookie

import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
 * 删除cookie
 *
 * @param name
 * @param response
 * @return
 */
public static void removeCookie(String name, String domain, HttpServletResponse response) {
    Cookie cookie = new Cookie(name, null); // 新建Cookie
    cookie.setDomain(domain); // 设置域名
    cookie.setMaxAge(0); // 设置有效期
    cookie.setPath("/"); //设置路径,这个路径即该工程下都可以访问该cookie 如果不设置路径,那么只有设置该cookie路径及其子路径可以访问
    response.addCookie(cookie); // 输出到客户端
}
 
开发者ID:lupindong,项目名称:xq_seckill_microservice,代码行数:15,代码来源:CookieUtil.java

示例4: setSessionCookie

import javax.servlet.http.Cookie; //导入方法依赖的package包/类
public static void setSessionCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, String domain, String path) {
    if (logger.isDebugEnabled()) {
        logger.debug("#login_setCookie " + name + " " + value + " " + path);
    }
    if (value == null){
        value = StringUtils.EMPTY;
    }
    if ("".equals(path)){
        path = "/";
    }
    Cookie cookie = new Cookie(name, value);
    if(StringUtils.isNotBlank(domain)){
        cookie.setDomain(domain);
    }
    cookie.setPath(path);
    addSecureParam(request, cookie);
    response.addCookie(cookie);
}
 
开发者ID:bridgeli,项目名称:netty-socketio-demo,代码行数:19,代码来源:CookieUtils.java

示例5: doSetCookie

import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
 * 设置Cookie的值,并使其在指定时间内生效
 * 
 * @param cookieMaxage cookie生效的最大秒数
 */
private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,
        String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) {
    try {
        if (cookieValue == null) {
            cookieValue = "";
        } else if (isEncode) {
            cookieValue = URLEncoder.encode(cookieValue, "utf-8");
        }
        Cookie cookie = new Cookie(cookieName, cookieValue);
        if (cookieMaxage > 0)
            cookie.setMaxAge(cookieMaxage);
        if (null != request) {// 设置域名的cookie
        	String domainName = getDomainName(request);
        	System.out.println(domainName);
            if (!"localhost".equals(domainName)) {
            	cookie.setDomain(domainName);
            }
        }
        cookie.setPath("/");
        response.addCookie(cookie);
    } catch (Exception e) {
    	 e.printStackTrace();
    }
}
 
开发者ID:mmdsyl,项目名称:BLOG-Microservice,代码行数:30,代码来源:CookieUtils.java

示例6: doSetCookie

import javax.servlet.http.Cookie; //导入方法依赖的package包/类
private Controller doSetCookie(String name, String value, int maxAgeInSeconds, String path, String domain, Boolean isHttpOnly) {
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(maxAgeInSeconds);
        // set the default path value to "/"
        if (path == null) {
            path = "/";
        }
        cookie.setPath(path);

        if (domain != null) {
            cookie.setDomain(domain);
        }
//        if (isHttpOnly != null) {
//            cookie.setHttpOnly(isHttpOnly);
//        }
        response.addCookie(cookie);
        return this;
    }
 
开发者ID:Xlongshu,项目名称:EasyController,代码行数:19,代码来源:Controller.java

示例7: removeCookieByName

import javax.servlet.http.Cookie; //导入方法依赖的package包/类
public static void removeCookieByName(HttpServletRequest request, HttpServletResponse response, String cookieName) {
    if (cookieName == null) {
        return;
    }
    Cookie[] cookies = request.getCookies();
    if (null == cookies || cookies.length == 0) {
        return;
    }
    for (Cookie thisCookie : cookies) {
        if (thisCookie.getName().equals(cookieName)) {
            thisCookie.setMaxAge(0); // 删除这个cookie
            thisCookie.setPath(HISTORY_PATH);
            if (checkDomain(request)) {
                thisCookie.setDomain(Constants.DOMAIN.getConstants());
            }
            response.addCookie(thisCookie);
        }
    }
}
 
开发者ID:tong12580,项目名称:OutsourcedProject,代码行数:20,代码来源:CookieUtil.java

示例8: setCookie

import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
 * 创建cookie
 * 
 * @param response
 * @param nameValues 存入cookie的键值对
 * @param hours 设置cookie的有效期(为空时随浏览器销毁)
 */
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, Integer hours, Boolean isHttpOnly) {
	try {
		if (value!=null){
			value = java.net.URLEncoder.encode(value, "UTF-8");
		}
	}
	catch (UnsupportedEncodingException e) {
		logger.warn("生成cookie[{}]失败。{}", name, e.getMessage());
	}
	Cookie cookie = new Cookie(name, value);
	if (null != hours) {
		//(生成缓存文件,否则在内存中)
		cookie.setMaxAge(hours * 60 * 60);
		cookie.setPath("/");
	}
	if (null != isHttpOnly) {
		cookie.setHttpOnly(isHttpOnly);
	}
	String domain = request.getServerName();
	boolean isIp = StringUtil.isIpAddress(domain);
	if (!isIp){
		String[] dis = domain.split(".");
		if (dis.length>=2){
			cookie.setDomain("."+ dis[dis.length-2]+"."+ dis[dis.length-1]);
		}
	}
	response.addCookie(cookie);
}
 
开发者ID:lemon-china,项目名称:lemon-framework,代码行数:36,代码来源:CookieUtil.java

示例9: shouldConvertServletCookieToJaxRsCookie

import javax.servlet.http.Cookie; //导入方法依赖的package包/类
@Test
public void shouldConvertServletCookieToJaxRsCookie() {
    final Cookie given = new Cookie("myCookie", "myValue");
    given.setDomain("example.com");
    given.setPath("/path");
    given.setMaxAge(1800);
    given.setHttpOnly(true);
    given.setSecure(true);

    final javax.ws.rs.core.Cookie expected = new javax.ws.rs.core.Cookie("myCookie", "myValue", "/path",
        "example.com");

    assertThat(CredentialFlowStateHelper.toJaxRsCookie(given)).isEqualTo(expected);
}
 
开发者ID:syndesisio,项目名称:syndesis,代码行数:15,代码来源:CredentialFlowStateHelperTest.java

示例10: addCookie

import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
 * 添加cookie
 * 
 * @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();
	}
}
 
开发者ID:wenjian-li,项目名称:spring_mybatis_shiro,代码行数:47,代码来源:CookieUtils.java

示例11: create

import javax.servlet.http.Cookie; //导入方法依赖的package包/类
public static void create(HttpServletResponse httpServletResponse, String name, String value, Boolean secure, Integer maxAge, String domain) {
    Cookie cookie = new Cookie(name, value);
    cookie.setSecure(secure);
    cookie.setHttpOnly(true);
    cookie.setMaxAge(maxAge);
    cookie.setDomain(domain);
    cookie.setPath("/");
    httpServletResponse.addCookie(cookie);
}
 
开发者ID:hellokoding,项目名称:single-sign-on-out-auth-jwt-cookie-redis-springboot-freemarker,代码行数:10,代码来源:CookieUtil.java

示例12: clear

import javax.servlet.http.Cookie; //导入方法依赖的package包/类
public static void clear(HttpServletResponse httpServletResponse, String name) {
    Cookie cookie = new Cookie(name, null);
    cookie.setPath("/");
    cookie.setHttpOnly(true);
    cookie.setMaxAge(0);
    cookie.setDomain("localhost");
    httpServletResponse.addCookie(cookie);
}
 
开发者ID:hellokoding,项目名称:single-sign-on-out-resources-jwt-cookie-redis-springboot-freemarker,代码行数:9,代码来源:CookieUtil.java

示例13: addCookie

import javax.servlet.http.Cookie; //导入方法依赖的package包/类
private void addCookie(HttpServletResponse response,String key,String value) throws Exception{
    Cookie cookie = new Cookie(key, URLEncoder.encode(value,"utf-8"));
    cookie.setDomain("localhost");
    cookie.setPath("/");
    cookie.setMaxAge(60*60*24*6);
    response.addCookie(cookie);
}
 
开发者ID:chang20159,项目名称:Instant-Messaging,代码行数:8,代码来源:WebSocketController.java

示例14: testCookieRetrieve

import javax.servlet.http.Cookie; //导入方法依赖的package包/类
@Test
public void testCookieRetrieve() {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final Cookie cookie = new Cookie("test", "test");
    cookie.setDomain("cas.org");
    cookie.setMaxAge(5);
    request.setCookies(new Cookie[] {cookie});

    assertEquals("test", this.g.retrieveCookieValue(request));
}
 
开发者ID:luotuo,项目名称:cas4.0.x-server-wechat,代码行数:11,代码来源:CookieRetrievingCookieGeneratorTests.java

示例15: createSessionCookie

import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
 * Creates a new session cookie for the given session ID
 *
 * @param context     The Context for the web application
 * @param sessionId   The ID of the session for which the cookie will be
 *                    created
 * @param secure      Should session cookie be configured as secure
 */
public static Cookie createSessionCookie(Context context,
        String sessionId, boolean secure) {

    SessionCookieConfig scc =
        context.getServletContext().getSessionCookieConfig();

    // NOTE: The priority order for session cookie configuration is:
    //       1. Context level configuration
    //       2. Values from SessionCookieConfig
    //       3. Defaults

    Cookie cookie = new Cookie(
            SessionConfig.getSessionCookieName(context), sessionId);
   
    // Just apply the defaults.
    cookie.setMaxAge(scc.getMaxAge());
    cookie.setComment(scc.getComment());
   
    if (context.getSessionCookieDomain() == null) {
        // Avoid possible NPE
        if (scc.getDomain() != null) {
            cookie.setDomain(scc.getDomain());
        }
    } else {
        cookie.setDomain(context.getSessionCookieDomain());
    }

    // Always set secure if the request is secure
    if (scc.isSecure() || secure) {
        cookie.setSecure(true);
    }

    // Always set httpOnly if the context is configured for that
    if (scc.isHttpOnly() || context.getUseHttpOnly()) {
        cookie.setHttpOnly(true);
    }
   
    String contextPath = context.getSessionCookiePath();
    if (contextPath == null || contextPath.length() == 0) {
        contextPath = scc.getPath();
    }
    if (contextPath == null || contextPath.length() == 0) {
        contextPath = context.getEncodedPath();
    }
    if (context.getSessionCookiePathUsesTrailingSlash()) {
        // Handle special case of ROOT context where cookies require a path of
        // '/' but the servlet spec uses an empty string
        // Also ensure the cookies for a context with a path of /foo don't get
        // sent for requests with a path of /foobar
        if (!contextPath.endsWith("/")) {
            contextPath = contextPath + "/";
        }
    } else {
        // Only handle special case of ROOT context where cookies require a
        // path of '/' but the servlet spec uses an empty string
        if (contextPath.length() == 0) {
            contextPath = "/";
        }
    }
    cookie.setPath(contextPath);

    return cookie;
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:72,代码来源:ApplicationSessionCookieConfig.java


注:本文中的javax.servlet.http.Cookie.setDomain方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。