Handling Cookies in JSP (II)
31 March 2008This 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.
Related Posts:
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)