public static String getCookieValue(Cookie[] cookies, String cookieName, String defaultValue) { for (int i = 0; i < cookies.length; i++) { Cookie cookie = cookies[i]; if (cookieName.equals(cookie.getName())) return (cookie.getValue()); } return (defaultValue); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); Tools.sendDisableCacheHeaders(response); final PrintWriter out = response.getWriter(); out.println("<html>"); out.println(" <body>"); Cookie cookie = getCookie("carlos-cookie-test", request); if (cookie == null) { print(out, "No cookie set."); } else { print(out, "<b>Cookie ID</b>: " + cookie.getName() + "<br>"); print(out, "<b>Value</b>: " + cookie.getValue() + "<br>"); refreshCookie(cookie, response); } out.println(" <br>"); out.println(" <form method=\"post\">"); out.println(" <input type=\"submit\" value=\"create\" name=\"button\"><br><br>"); out.println(" <input type=\"submit\" value=\"delete\" name=\"button\"><br><br>"); out.println(" <input type=\"submit\" value=\"no-pass\" name=\"button\"><br>"); out.println(" </form>"); out.println(" </body>"); out.println("</html>"); out.close(); }
// getBrowserInfiniteCookie public static String getBrowserInfiniteCookie(HttpServletRequest request) { Cookie[] cookieJar = request.getCookies(); if (cookieJar != null) { for (Cookie cookie : cookieJar) { if (cookie.getName().equals("infinitecookie")) { return cookie.getValue() + ";"; } } } return null; } // TESTED
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { ArrayList<String> ar = new ArrayList<String>(); boolean flag = false; Cookie[] cArr = req.getCookies(); if (cArr != null) { for (int i = 0; i < cArr.length; i++) { Cookie c0 = cArr[i]; if (c0.getName().equals("Name") && !c0.getValue().equals("Logout")) { res.sendRedirect("index.html"); flag = true; } } } if (flag == false) res.sendRedirect("Login.html"); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Shared Info"; out.println( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">" + "<HTML>\n" + "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" + "<UL>\n" + " <LI>Session:"); HttpSession session = request.getSession(true); Enumeration attributes = session.getAttributeNames(); out.println(getAttributeList(attributes)); out.println(" <LI>Current Servlet Context:"); ServletContext application = getServletContext(); attributes = application.getAttributeNames(); out.println(getAttributeList(attributes)); out.println(" <LI>Servlet Context of /shareTest1:"); application = application.getContext("/shareTest1"); if (application == null) { out.println("Context sharing disabled"); } else { attributes = application.getAttributeNames(); out.println(getAttributeList(attributes)); } out.println(" <LI>Cookies:<UL>"); Cookie[] cookies = request.getCookies(); if ((cookies == null) || (cookies.length == 0)) { out.println(" <LI>No cookies found."); } else { Cookie cookie; for (int i = 0; i < cookies.length; i++) { cookie = cookies[i]; out.println(" <LI>" + cookie.getName()); } } out.println(" </UL>\n" + "</UL>\n" + "</BODY></HTML>"); }