Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


Java Tutorials



Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Jsp Jspcookies

User Comments
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl
  Blink   Furl   Simpy   Y! MyWeb 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

30 comments so far (post your own) View All Comments Latest 10 Comments:

Thank you for your material. It really helped.Can you please give some examples based on servlet.

Posted by deep on Thursday, 01.21.10 @ 09:44am | #94190

there are very brief explanation of cookies and how the cookies are created.


Posted by altaf khan on Wednesday, 10.14.09 @ 15:22pm | #91754

These lines are redundant, the variables are never used:

Date now = new Date();
String timestamp = now.toString();

Posted by Sam Hasler on Saturday, 09.26.09 @ 17:39pm | #91217

Please the dCOUMENT

Posted by Ravi Prasad on Monday, 07.6.09 @ 15:34pm | #89074

i am working as a lecturer in st. xavier's polytechnic college-sydunganallur. this site is very useful to me to know about the cookie concept of JSP

Posted by Karthika on Sunday, 02.8.09 @ 22:29pm | #84621

I am facing problem with setMaxAge().It is not keeping the cookie for 1 year.

Posted by Sreedhar L on Sunday, 01.25.09 @ 04:56am | #84143

Very good, short, and simple.
almost started using in 5 mins.

Posted by kishore Borate on Thursday, 01.22.09 @ 01:58am | #84045

cookies codes

Posted by vinotjh on Monday, 12.29.08 @ 06:51am | #83258

i just use the cookie example,i use 3 ie to run a single program with various input..
While use of cookie ,the page should show the various outputs...
but it shows the value which is submit last in all 3 pages....
so the example cookie not give correct answer...

Posted by edwin on Friday, 12.5.08 @ 23:41pm | #82459

i just learn the basic, and the examples are very useful to understand easily....

Posted by edwin on Friday, 12.5.08 @ 23:01pm | #82456

Why I can't see some cookie in my code despites I see him on the browser ?
I use an jsp code to show every cookie in the request by cookies = request.getCookies , then a block with a for and somo System.out.println And it shows me some, but not every cookie.
Even Charles shows me all the cookies in the request.
all cookies send in the request are in the seme domain as mine and even paths are set to /and MaxAge = -1
For your help thanks a a lot!

Posted by Felipe on Monday, 11.17.08 @ 19:28pm | #81759

Very nice explanation...

Posted by Dhruvi on Tuesday, 09.9.08 @ 18:46pm | #78312

This is very good site.This site very useful to me.
Giving very good example with synataxes

Posted by srinu on Friday, 08.8.08 @ 12:49pm | #71765

Hi all,

This article is very useful. And this site is very nice. And one more thing , here you have done like tracking all cookies instead of one cookie. But my question is how to retrieve single cookie which is set before.

like in session session.getAttribute("sessionName");

If anybody knows this please mail me

my mail is achchayya@gmail.com

Thanks in advance

Posted by zimbu on Friday, 07.4.08 @ 12:54pm | #65633

Concept of Cookies is very Good in this Web Page

Posted by Anindya on Tuesday, 07.1.08 @ 13:12pm | #65210

Thank you very much! I like this one a lot as it helps me easily to understand regarding Cookie in JSP.

Posted by Binh Nguyen on Thursday, 06.26.08 @ 11:59am | #64654

This is very simple and good understandable example.

Posted by Manohar on Thursday, 06.12.08 @ 11:17am | #63092

it is really an interesting and easy way of learning JSP..but one thing i would like to say here.how we can view the more the one values stored inside the cookie.while displaying the third page....please upload more and more samples on Advance JSP..Thanks Alot..........

Posted by ISMAIL on Wednesday, 04.30.08 @ 15:45pm | #58215

There is one flaw.Dont enter any value in the first page.Press submit than press on the link.

It is showing "welcome:."
It should show error message.

Posted by Krunal on Sunday, 03.30.08 @ 00:14am | #54684

Cookie Class In JSP cookie are the object of the class javax.servlet.http.Cookie. This class is used to creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management. A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. The getCookies() method of the request object returns an array of Cookie objects. Cookies can be constructed using the following code: Cookie(java.lang.String name, java.lang.String value) Cookie objects have the following methods. Method Description getComment() Returns the comment describing the purpose of this cookie, or null if no such comment has been defined. getMaxAge() Returns the maximum specified age of the cookie. getName() Returns the name of the cookie. getPath() Returns the prefix of all URLs for which this cookie is targeted. getValue() Returns the value of the cookie. setComment(String) If a web browser presents this cookie to a user, the cookie's purpose will be described using this comment. setMaxAge(int) Sets the maximum age of the cookie. The cookie will expire after that many seconds have passed. Negative values indicate the default behavior: the cookie is not stored persistently, and will be deleted when the user web browser exits. A zero value causes the cookie to be deleted setPath(String) This cookie should be presented only with requests beginning with this URL. setValue(String) Sets the value of the cookie. Values with various special characters (white space, brackets and parentheses, the equals sign, comma, double quote, slashes, question marks, the "at" sign, colon, and semicolon) should be avoided. Empty values may not behave the same way on all browsers. Example Using Cookies No we will write code in JSP file to set and then display the cookie. Create Form Here is the code of the form (cookieform.jsp) which prompts the user to enter his/her name. <%@ page language="java" %> <html> <head> <title>Cookie Input Form</title> </head> <body> <form method="post" action="setcookie.jsp"> <p><b>Enter Your Name: </b><input type="text" name="username"><br> <input type="submit" value="Submit"> </form> </body> Above form prompts the user to enter the user name. User input are posted to the setcookie.jsp file, which sets the cookie. Here is the code of setcookie.jsp file:
<%@ 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); %>

Posted by Parameter on Wednesday, 03.12.08 @ 22:53pm | #52502

it was really nice . thank u ! but one thing

i would like to say here first of all enter user name after that it will show the next link page and then again clicking that link it will show the saved cookie . but whenever again whenever i entered a new cookie it won't displaying the old cookie , it simply showing the new cookie . i got confused so please help me.

Posted by dasari on Friday, 02.15.08 @ 12:42pm | #48371

Thanks for tutorial. It is easy for understanding.

Posted by Phong Tran on Wednesday, 02.13.08 @ 11:54am | #48125

this is very nice and useful thanq

Posted by deepthi on Wednesday, 12.19.07 @ 14:51pm | #42948

Its nice and easy. Thanks for giving this tutorial.

Posted by suresh on Tuesday, 12.18.07 @ 18:39pm | #42840

this examples help me lot for doing projects and learn, like this I want example on jsp how to give link pdf files, in project when we click on the help button to display the pdf files

Posted by majjikishore on Tuesday, 11.13.07 @ 15:59pm | #37224

this will help me a lot to make my school project, it's about a shopping website.

thank you for upload this kind of examples, they are very useful!!!

i wonder if you can send me to my email an example where there are more than just one value to write in the cookie: for example, a part number, brand, name of the product, price...

my email, again, is the following:

i hope you could answer me!!! thanks a lot


Posted by edgar on Saturday, 10.6.07 @ 04:22am | #31897

this example is very simple and good for understanding the basics of cookies.

Posted by vivek on Tuesday, 10.2.07 @ 16:32pm | #30952

Really easy to understand so far. Thanks a lot ^^

Posted by Victor on Wednesday, 09.5.07 @ 12:59pm | #25075

Easy and simple with example to learn cookie in JSP.
Thanks for article.

Posted by Sohel on Wednesday, 07.11.07 @ 14:44pm | #21079

It was very simple and really nice.It helped me a lot.Thank you.

Posted by Renjit Clement on Thursday, 05.24.07 @ 17:24pm | #17159

Ask programming questions?



Back to Tutorial


  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name


Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.