Get Cookies Servlet : Cookie « Servlet « Java Tutorial






import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetCookiesServlet extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    Cookie[] cookies = request.getCookies();

    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    pw.println("<B>");
    for (int i = 0; i < cookies.length; i++) {
      String name = cookies[i].getName();
      String value = cookies[i].getValue();
      pw.println("name = " + name + "; value = " + value);
    }
    pw.close();
  }
}








25.4.Cookie
25.4.1.Add Cookie Servlet
25.4.2.Get Cookies Servlet
25.4.3.Servlet Cookie Setter
25.4.4.Servlet Cookie Reader
25.4.5.Get/Set Cookie
25.4.6.List Servlet Cookie Information
25.4.7.Parse a Cookie: header into individual tokens according to RFC 2109.