title: java servlet get cookie value in JSP
body: <p>In java servlet I have following code:</p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {       
      Cookie cookie = new Cookie("tom_cookies",Long.toString(new Date().getTime()));
      cookie.setMaxAge(30);
      cookie.setPath(request.getContextPath());
      cookie.setComment("1");
      cookie.setVersion(1);
      System.out.println("Cookie created!");
      response.addCookie(cookie);
  }
  </code></pre>
  
  <p>In JSP index.jsp I have following code:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;MyIndex&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div&gt;CookieComment: &lt;%
  Cookie[] my = request.getCookies();
  for(int i=0;i&lt;my.length;i++){
  String comment = my[i].getComment();
  out.println(comment);
  }
  %&gt; 
  &lt;/div&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>My web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;servlet&gt;
  &lt;servlet-name&gt;Test&lt;/servlet-name&gt;
  &lt;servlet-class&gt;Servlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
  &lt;servlet-name&gt;Test&lt;/servlet-name&gt;
  &lt;url-pattern&gt;/servlet1&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;display-name&gt;1aaa&lt;/display-name&gt;
  &lt;welcome-file-list&gt;
  &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
  &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
  &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
  &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
  &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
  &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>In local:8080/servlet, what I expect is like:</p>
  
  <blockquote>
    <p>CookieComment: 1</p>
  </blockquote>
  
  <p>However, it only shows:</p>
  
  <blockquote>
    <p>CookieComment: null</p>
  </blockquote>
  
  <p>whats wrong here?</p>