0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Spring】Cookie操作

0
Posted at

概要

当記事では、Cookieの
  ・保存
  ・取得
の実装方法について説明します。
※実装自体難しくないため、
 実装例を先に挙げ、後半で解説します。

実装例

/CookieDemo/src/main/java/com/example/demo/CookieController.java
package com.example.demo;

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

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("cookie")
public class CookieController {

	/**
	 * 登録アクション
	 */
	@RequestMapping(value = "/registration", method = RequestMethod.GET)
	public String registrationAction(
		@RequestParam(required = false) String cookieValue,
		HttpServletResponse response,
		Model model) {

		Cookie cookie = new Cookie("id", cookieValue); // オブジェクト作成(キー、バリューを指定)
		cookie.setMaxAge(3600); // 有効期間(単位:秒)
		response.addCookie(cookie);

		model.addAttribute("cookieValue", cookieValue);

		return "registration";
	}

	/**
	 * 参照アクション
	 */
	@RequestMapping(value = "/show", method = RequestMethod.GET)
	public String showAction(
		@CookieValue(value = "id", required = false) String cookieValue,
		Model model) {

		model.addAttribute("cookieValue", cookieValue);

		return "show";
	}

}
/CookieDemo/src/main/resources/templates/registration.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CookieDemo - registration</title>
</head>
<body>
cookieを保存しました。
[[${cookieValue}]]
</body>
</html>
/CookieDemo/src/main/resources/templates/show.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CookieDemo - show</title>
</head>
<body>
[[${cookieValue}]]
</body>
</html>

解説

CookieController

registrationAction

Cookieを保存するアクションです。

下記の実装でCookieオブジェクトを作成/設定し、
保存するCookieの情報を生成しています。

Cookie cookie = new Cookie("id", cookieValue); // オブジェクト作成(キー、バリューを指定)
cookie.setMaxAge(3600); // 有効期間(単位:秒)

その後の

response.addCookie(cookie);

でCookieの保存をしています。

showAction

Cookieの内容を表示するアクションです。

「CookieValue」のアノテーションを付与することで、
Cookieの情報を取得しています。

動作検証

Cookieを保存するため、
下記のURLをブラウザでアクセスします。
http://localhost:8080/cookie/registration?cookieValue=test12345
image.png

Cookieの保存内容を確認するため、
下記のURLをブラウザでアクセスします。
※registrationで保存したCookieの値「test12345」が表示されます。
http://localhost:8080/cookie/show

image.png

補足

Cookieの削除について

registrationActionで記載した

cookie.setMaxAge(3600); // 有効期間(単位:秒)

の秒数部分にゼロをセットすることで削除と同等の実装をすることができます。

Cookieの保存時の各設定について

サーバのパス(setPath)やドメイン(setDomain)を設定することも可能です。
必要に応じて適切に設定してください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?