title: How do I turn off JSESSIONID in Tomcat/Spring Boot when I am only sending a redirect?
body: <p>How do I stop Tomcat from sending <code>JSESSIONID</code> cookie when I am only sending a redirect? I don't access <code>.getSession()</code> at all.</p>
  
  <pre><code>@RequestMapping(value = "/*")
  public void doUrlRequest(final HttpServletRequest request, HttpServletResponse response) {
    ...
    if (redirect != null) response.sendRedirect(redirect);
  </code></pre>
  
  <p>Console</p>
  
  <pre><code>$ curl -i localhost:8080/aaa                                                                                                                                                                               HTTP/1.1 302
  Set-Cookie: JSESSIONID=A38FE0B0703F4241414BD8DEF646A2BA; Path=/; HttpOnly
  Location: http://localhost:8080/errorNonMobile
  Content-Length: 0
  Date: Thu, 17 May 2018 19:55:49 GMT
  </code></pre>
  
  <p>I tried</p>
  
  <pre><code>request.getServletContext().setSessionTrackingModes(Collections.singleton(SessionTrackingMode.URL));
  </code></pre>
  
  <p>But it errored</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException:
    The session tracking modes for context [] cannot be set whilst the context is running</p>
  </blockquote>
  
  <p>I need session for another path. I cannot turn it off for the whole app. I only want to turn it off for this request handler.</p>