package devmanuals; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class GetCookie extends HttpServlet { PrintWriter pw = null; public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doPost(req, res); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { pw = res.getWriter(); String username = null; // Retrieve the cookies, if any, stored in the client browser Cookie ck[] = req.getCookies(); if (ck != null) { for (int i = 0; i < ck.length; i++) { if (ck[i].getName().equals("user")) // Retrieve username from the cookie username = ck[i].getValue(); } res.setContentType("text/html"); pw.println(" Hello!   " + username); } else { pw.println("No cookies found"); } } }