Setting and Reading Cookies : Cookie « JSP « Java

Setting and Reading Cookies

        <TITLE>Setting and Reading Cookies</TITLE>
        Cookie[] cookies = request.getCookies();
        boolean foundCookie = false;

        for(int i = 0; i < cookies.length; i++) { 
            Cookie c = cookies[i];
            if (c.getName().equals("color")) {
                out.println("bgcolor = " + c.getValue());
                foundCookie = true;

        if (!foundCookie) {
            Cookie c = new Cookie("color", "cyan");
        <H1>Setting and Reading Cookies</H1>
        This page will set its background color using a cookie.


Related examples in the same category

1.JSP Create and List Cookie
2.JSP List All Cookie
3.Setting a Cookie
4.Deal with the cookie
5.Cookie display in a JSP page
6.JSP: deal with cookie