title: getCookies() gets both the cookies and the session
body: <p>I have two servlets and the other servlet is supposed to output information from a cookie and from a session (instructions of our professor). The problem is, I tried using getCookies() to get cookies from the request object but it returns two objects the first one is with a name "JSESSIONID" and its accompanied hexadecimal code and the second one contains the cookie I created. Why did this happen? How would getCookie() solely return cookies?</p>
  
  <p>Here's the code:</p>
  
  <p>Servlet 1:</p>
  
  <pre><code>package com.telco.process;
  
  import java.io.IOException;
  
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.Cookie;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  /**
   * Servlet implementation class ConvertServlet
   */
  @WebServlet("/Convert")
  public class ConvertServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
      private String num;
      private String telco;
      private HttpSession session;
      private RequestDispatcher rd;
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
      {
          String convertedNum = "";
          session = request.getSession();
          num = request.getParameter("number");
          telco = request.getParameter("telco");
          rd = request.getRequestDispatcher("Display");
  
          Cookie cTelco = new Cookie("telco", telco);
          if (!(num.equals(null)) || !(num.equals("")))
          {
              for (byte i = 0; i &lt; num.length(); i++)
              {
                  switch(num.charAt(i))
                  {
                      case 'A':
                      case 'B':
                      case 'C': convertedNum += "2"; break;
                      case 'D': 
                      case 'E':
                      case 'F': convertedNum += "3"; break;
                      case 'G':
                      case 'H': 
                      case 'I': convertedNum += "4"; break;
                      case 'J':
                      case 'K':
                      case 'L': convertedNum += "5"; break;
                      case 'M':
                      case 'N':
                      case 'O': convertedNum += "6"; break;
                      case 'P':
                      case 'Q':
                      case 'R':
                      case 'S': convertedNum += "7"; break;
                      case 'T':
                      case 'U':
                      case 'V': convertedNum += "8"; break;
                      case 'W':
                      case 'X':
                      case 'Y':
                      case 'Z': convertedNum += "9"; break;
                  }
              }
              session.setAttribute("number", convertedNum);
              response.addCookie(cTelco);
          }
          rd.forward(request, response);
      }
  
  }
  </code></pre>
  
  <p>Servlet 2:</p>
  
  <pre><code>package com.telco.process;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.Cookie;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  @WebServlet("/Display")
  public class DisplayNumber extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          response.setContentType("text/html");
          PrintWriter pw = response.getWriter();
          Cookie ck[] = request.getCookies();
          System.out.println(ck[0].getValue());
          pw.println("The converted number is " + "-" + request.getSession(false).getAttribute("number"));    
      }
  }
  </code></pre>