Setting a Cookie : Cookie « JSP « Java






Setting a Cookie

//File: createCookie.jsp
<HTML>
    <HEAD>
        <TITLE>Setting a Cookie</TITLE>
    </HEAD>

    <BODY> 
        <H1>Setting a Cookie</H1>
  
        <%
        Cookie cookie1 = new Cookie("message", "Hello!");
        cookie1.setMaxAge(24 * 60 * 60);
        response.addCookie(cookie1); 
        %> 
    
        <A HREF="readCookie.jsp"/>Read the cookie</A> 
    </BODY>
</HTML>

//File: readCookie.jsp
<HTML>
    <HEAD>
        <TITLE>Reading a Cookie</TITLE>
    </HEAD>  

    <BODY>
        <H1>Reading a Cookie</H1>  
  
        <%
        Cookie[] cookies = request.getCookies();
    
        for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
            if (cookies[loopIndex].getName().equals("message")) {
                out.println("The cookie says " + cookies[loopIndex].getValue());
            }
        }  
        %>
    </BODY>  
</HTML> 

           
       








JspCreateReadCookie.zip( 89 k)

Related examples in the same category

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