단순 코드 기록/Spring

Spring_Cookie

일일일코_장민기 2024. 2. 13. 10:29
728x90
Controller
package com.app.test;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	
	@RequestMapping(value = "/")
	public String home() {
		System.out.println("hello");
		
		return "hello";
	}
	
	@RequestMapping(value = "/setCookie")
	public String SetCookie(HttpServletRequest request, HttpServletResponse response) {
		Cookie c1 = new Cookie("myName", "홍길동");
		Cookie c2 = new Cookie("age", "10");
		Cookie c3 = new Cookie("favorite", "악기");
		c1.setMaxAge(60*60);
		c2.setMaxAge(60*60);
		c3.setMaxAge(60*60);
		response.addCookie(c1);
		response.addCookie(c2);
		response.addCookie(c3);
		System.out.println("set Cookie");
		
		return "hello";
	}

	@RequestMapping(value = "/getCookie")
	public void GetCookie(HttpServletRequest request, HttpServletRequest response) {
		Cookie[] cookies = request.getCookies();
		for (Cookie cookie : cookies) {
			System.out.println(cookie.getName()+"\t"+cookie.getValue());
		}
		System.out.println("set Cookie");
		
	}
}

 

 

 

'단순 코드 기록 > Spring' 카테고리의 다른 글

Spring_Filter  (0) 2024.02.13
Spring_ViewerParsing  (0) 2024.02.13
Spring_Parsing  (0) 2024.02.08
Spring_Multi_controller  (0) 2024.02.08
Spring_Root  (0) 2024.02.08