Spring MVC
Cookies are small text files that are stored on client computers browser directory.
Cookies can be used for any of the following
1. Remember the information about the user who has visited a website, so that when the user returns to the website in future they can show information useful to the user,
Most of the shopping websites do this.
2. Track internet users web browsing.
3. Used in Session management.
Spring MVC Cookie handling
Spring MVC provides methods which we can use to create cookies or read data from cookies easily.
In this article we will see the tips for Spring MVC Cookie handling.
Read data from Cookie
Using the @Cookie annotaion we can define the cookie name that we want to read.
1 2 3 4 5 |
@RequestMapping("/readCookie") public String readCookieValue(@CookieValue("myCookie") String myCookie) { // .... return "readCookie"; } |
In the above example we may get an exception if the cookie with the given name is not found by Spring.
So It is recommended to add default a value to the cookie that we want to read to avoid any exceptions.
1 |
public String readCookieValue(@CookieValue(value="myCookie" , defaultValue ="test123") String myCookie) { |
Store Data in Cookie
For storing the cookie value we use the response object.
1 2 3 4 5 6 7 |
@RequestMapping("/writeCookie") public String writeCookieValue(HttpServletResponse response) { response.addCookie(new Cookie("myCookie", "Cookie From KS Codes")); return "writeCookie"; } |
I have created a simple example to read and write data in Cookie.
In this example we will have 2 links on the home page, one to read and other to write data in cookie.
If we first click on Read Cookie link, the default cookie value that was passed as param to the controller will be set in the cookie.
If we click on the Write Cookie link and then Read the Cookie value , then the new Cookie value will be displayed.
CookieController,java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.kscodes.cookiehandling; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class CookieController { @RequestMapping("/home") public String getHomePage() { return "home"; } @RequestMapping("/readCookie") public ModelAndView readCookieValue( @CookieValue(value = "myCookie", defaultValue = "test123") String myCookie) { ModelAndView mv = new ModelAndView("readCookie"); mv.addObject("myCookie", myCookie); return mv; } @RequestMapping("/writeCookie") public String writeCookieValue(HttpServletResponse response) { response.addCookie(new Cookie("myCookie", "Cookie From KS Codes")); return "writeCookie"; } } |
OutPut
First Scenario : Clicking on Read Cookie without setting cookie value
Second Scenario : Clicking on Read Cookie after setting cookie value