*/ -->

前回(GoogleAppEngineでCookieを利用する)のコードを修正しました的なメモ。

  • expires属性をMax-Age属性に
  • コンストラクタ追加
import javax.servlet.http.Cookie;

public class SimpleCookie
{
	private String name;
	private String value;

	private int age = -1;
	private String domain = null;
	private String path = null;
	private boolean secure = false;

	public SimpleCookie(String name, String value)
	{
		this.name = name;
		this.value = value;
	}

	public SimpleCookie(Cookie cookie)
	{
		this.age = cookie.getMaxAge();
		this.domain = cookie.getDomain();
		this.name = cookie.getName();
		this.path = cookie.getPath();
		this.secure = cookie.getSecure();
		this.value = cookie.getValue();
	}

	@Override public String toString()
	{
		StringBuilder builder = new StringBuilder();

		// key value
		builder.append(this.name).append("=").append(this.value).append(";");

		if(this.age != -1)
		{
			builder.append("Max-Age=").append(this.age).append(";");
		}

		if(this.domain != null)
		{
			builder.append(" domain=").append(this.domain).append(";");
		}

		if(this.path != null)
		{
			builder.append(" path=").append(this.path).append(";");
		}

		if(this.secure)
		{
			builder.append(" secure");
		}

		return builder.toString();
	}

	public int getMaxAge()
	{
		return age;
	}

	public void setMaxAge(int expires)
	{
		this.age = expires;
	}

	public String getDomain()
	{
		return domain;
	}

	public void setDomain(String domain)
	{
		this.domain = domain;
	}

	public String getPath()
	{
		return path;
	}

	public void setPath(String path)
	{
		this.path = path;
	}

	public boolean getSecure()
	{
		return secure;
	}

	public void setSecure(boolean secure)
	{
		this.secure = secure;
	}

	public String getName()
	{
		return name;
	}

	public String getValue()
	{
		return value;
	}
}

関連性がある記事

Disqusでコメント