Read a Cookie from a servletFrom WikiJava
This article contains a simple Servlet that prints out all the cookies sent from the client with the HTTP request.
the articleThis example uses the: Cookie[] request.getCookies() and Cookie.getName() Cookie.getValue() to retrieve the cookies information from the request object. MyServletimport javax.servlet.*; import javax.servlet.http.*; public class MyServlet extends HttpServlet { public void doPost (HttpServletRequest req, HttpServletResponse res) { Cookie[] theCookies = request.getCookies(); if (theCookies != null) { java.io.PrintWriter out = res.getWriter(); for (int i =0; i< cookies.length; i++) { Cookie aCookie = theCookies[i]; out.println ("Name : " + aCookie.getName() + " Value: " + aCookie.getValue()); } } } public void doGet(HttpServletRequest req, HttpServletResponse res) { doPost(req, res); } } See Also |
