Java 怎么将cookie设置为httpservletresponse

2019年8月17日22:01:53 评论 25

1.简介

Java 怎么将cookie设置为httpservletresponse?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Main{
    private final static String COOKIE_NAME = "fileDownload";
    private final static String COOKIE_VALUE = "true";
    private final static int COOKIE_EXPIRE_SECS = 10 * 60;
    
    public final static void setCookie(HttpServletRequest request,
            HttpServletResponse resp) {

        setCookie(request, resp, null, null);
    }
    
    public final static void setCookie(HttpServletRequest request,
            HttpServletResponse resp, String path, String domain) {

        Cookie c = new Cookie(COOKIE_NAME, COOKIE_VALUE);
        if (domain != null)
            c.setDomain(domain);

        if (path != null)
            c.setPath(path);

        
        c.setMaxAge(COOKIE_EXPIRE_SECS);
        resp.addCookie(c);
    }
}

 

3.总结

示例代码演示了 Java 怎么将cookie设置为httpservletresponse,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: