Tuesday, October 13, 2015

COOKIES IN JSP

SIMPLE COOKIES CREATION AND READING COOKIES

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
           
         
            Cookie cookie = null;
            String name = request.getParameter("uname");
       
            cookie = new Cookie("user", name);//creating cookie
            cookie.setMaxAge(60*60*24);
            response.addCookie(cookie);
            Cookie cook[]=request.getCookies();
            for(int i=0;i<cook.length;i++)  //reading cookies..
            {
                cookie=cook[i];
                out.println("COOKIE NAME.."+cookie.getName()+"</br>");
                out.println("COOKIE VALUE.."+cookie.getValue()+"</br>");
            }
            out.print(cookie);
            %>
    </body>
</html>

No comments:

Post a Comment