Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Saturday, December 1, 2007

How to JSP: deal with cookie in java

<%@ page import="javax.servlet.http.Cookie" %>



This page leaves a cookie



Cookies



<%
Cookie[] allCookies = request.getCookies();
Cookie ourCookie = null;
if (allCookies!=null)
{
for (int i=0; i {
if (allCookies[i].getName().equals("TestCookie"))
{
ourCookie = allCookies[i];
}
}
}

if (ourCookie == null)
{
Cookie cookie = new Cookie("TestCookie", "hello from cookie");
//cookie.setMaxAge(1800);
//cookie.setDomain("alex:8080");
cookie.setPath("/");
response.addCookie(cookie);
%>
A cookie has been added to your machine!

Select refresh to see the details of this cookie.

<%
}
else
{
%>
The following cookie was added earlier to your machine:

Version: <%=ourCookie.getVersion() %>

Name: <%=ourCookie.getName() %>

Value: <%=ourCookie.getValue() %>

MaxAge: <%=ourCookie.getMaxAge() %>
<%
}
%>


No comments: