Handling Cookies in JSP (II)

31 March 2008

This post is in continuation of Handling Cookies in JSP (I). Please do read that before this one.

I will continue the example presented in the first part of this post.
The page below gets the posted values using request object and stores it into a cookie. It then displays a link to showcookievalue.jsp, which shows the value stored in the cookie.

setcookie.jsp

<%@ page language="java" import="java.util.*"%> 
<% String username=request.getParameter("username"); 
if(username==null) username=""; 
Date now = new Date(); 
String timestamp = now.toString(); 
Cookie cookie = new Cookie ("username",username); 
cookie.setMaxAge(365 * 24 * 60 * 60); 
response.addCookie(cookie); %> 
<html> <head> <title>Cookie Saved</title> </head> 
<body> 
<p><a href="showcookievalue.jsp">Next Page to view the cookie value</a><p> 
</body>

The code presented is simple. We simply get the username from the posted form, and store the value in a cookie. Creating the cookie and setting its age is important here.

Do follow the next post on this.

del.icio.us:Handling Cookies in JSP (II)  digg:Handling Cookies in JSP (II)  spurl:Handling Cookies in JSP (II)  wists:Handling Cookies in JSP (II)  simpy:Handling Cookies in JSP (II)  newsvine:Handling Cookies in JSP (II)  blinklist:Handling Cookies in JSP (II)  furl:Handling Cookies in JSP (II)  reddit:Handling Cookies in JSP (II)  fark:Handling Cookies in JSP (II)  blogmarks:Handling Cookies in JSP (II)  Y!:Handling Cookies in JSP (II)  smarking:Handling Cookies in JSP (II)  magnolia:Handling Cookies in JSP (II)  segnalo:Handling Cookies in JSP (II)  gifttagging:Handling Cookies in JSP (II)

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Handling Cookies in JSP (II)

Leave a Reply