title: Set and read cookies in Spring MVC/Security
body: <p>I am new to Spring and I need to set a custom cookie when "Login" button is clicked, and then that cookie will be red inside the webapp. What is the best practice to do exactly that? Also, this brings question how to read it later on every page in webapp?</p>
  
  <p>I thought I can set cookies with JavaScript, and read it later via custom filter (which would read the cookie from request, set it to the attribute and send it to controller.</p>
  
  <p>Is this thought correct? Or should I set the cookie somewhere else (if so where and why?)</p>
  
  <p><strong>UPDATE 1:</strong> </p>
  
  <p>What I want to achieve: I have dropdown box (which is language selector) on login page that has some values (language code, e.g. "en"), and selected value needs to be set as cookie (e.g. "lang") and that "lang" cookie will be red for i18n later on the pages. I have made i18n to work, but I need to read "lang" cookie to set the selected language.</p>
  
  <p><strong>UPDATE 2:</strong> </p>
  
  <p>I have done what I wanted to do, but it is not exactly clean:</p>
  
  <p>I am setting cookie via Javascript, or jQuery to be exact, and when user selects or changes the selection in <code>&lt;select/&gt;</code> then Javascript injects language value as cookie (e.g. en):</p>
  
  <p><strong>HTML:</strong></p>
  
  <pre><code>&lt;select name="language" id="selectLanguage" class="form-control"&gt;
     &lt;option val="en"&gt;English&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p><strong>JS:</strong></p>
  
  <pre><code>var cookie = {
  
      set: function($this) {
          var now = new Date();
          var time = now.getTime();
          var expireTime = time + 1000*36000;
          now.setTime(expireTime);
          document.cookie = 'lang=' + $this.val() +';expires='+now.toGMTString()+';path=/';
      }
  
  }
  
  $('#selectLanguage').change(function(event) {
      cookie.set($(this));
  });
  </code></pre>
  
  <p>Then I created new <strong>Filer</strong> that I called <strong>CookieFilter.java</strong>:</p>
  
  <pre><code>public class CookieFilter implements Filter {
  
      @Override
      public void doFilter(ServletRequest req, ServletResponse res,
              FilterChain chain) throws IOException, ServletException {
          HttpServletRequest request = (HttpServletRequest) req;
  
          Cookie[] cookies = ((HttpServletRequest) req).getCookies();
          if (cookies != null) {
              for (Cookie ck : cookies) {
                  if(ck.getName().toString().equals("lang")){
                      req.setAttribute("languageCookie", ck.getValue());
                  } else {
                      req.setAttribute("languageCookie", "en");
                  };
              }
              chain.doFilter(req, res);
          }
      }
  
      public void init(FilterConfig config) throws ServletException {
          // TODO Auto-generated method stub
      }
      public void destroy() {
          // TODO Auto-generated method stub
      }
  }
  </code></pre>
  
  <p>Added new <strong>filter</strong> in <strong>web.xml</strong>:</p>
  
  <pre><code>&lt;filter&gt;
      &lt;filter-name&gt;CookieFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;
          package.path.to.CookieFilter
      &lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;CookieFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>And to finish everything, I got the request from <strong>CookieFilter</strong> in my controller and red the attribute I sent:</p>
  
  <pre><code>String cookie = request.getAttribute("languageCookie").toString();
  model.addAttribute("languageCookie",cookie);
  </code></pre>
  
  <p>Now I can read attribute from <code>model</code> and set it inside .JSP or do everything I want with it.</p>
  
  <p>That is my solution, but there must be other way ... :)</p>