Add Cookie 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 AddCookieServlet extends HttpServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String data = request.getParameter("data");
    Cookie cookie = new Cookie("MyCookie", data);
    response.addCookie(cookie);
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    pw.println("<B>MyCookie has been set to");
    pw.println(data);
    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.