Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
|
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 12th, 2007, 02:44 PM
Authorized User
 
Join Date: Mar 2005
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to arimakidd
Default Can Somone Explain How This Cookie Works?

I have a .jsp page that highlights the usefulness of cookies. This simple page asks you for your name and stores it in a cookie that lasts for about 1 hour. However the part I don't understand right now is how the cookie sets. This is during the evaluation of its existence on the client machine. Here is the code:
Code:
//Create the cookies object which is an array of cookies available to this page.
Cookie[] cookies = request.getCookies();

//Loop through the array and find the NameCookie cookie
for (int i = 0; i < cookies.length; i++) {
    nameCookie = cookies[i];
    if (nameCookie.getName().equals("NameCookie")) {
        cookieValue = nameCookie.getValue();

        //The user's been here before!
        cookieExists = true;
    }
}
Now, the cookies array? What are the contents of this array? I believe its checking to see if a cookie by the name of "nameCookie" exists? But what values is it referencing? So cookie.length is how long?

Just in case someone wants to go the marathon explanation here is the entire script. NOW!! Beware!!
This page gives some problems to run. I only got it to run in oracle's JDeveloper 9i. This ide is very easy and convenient to use I love it. Thanks in advance for the help.

 
Old May 14th, 2007, 10:34 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,

First of all we use "HttpServletResponse.addCookie(javax.servlet.http. Cookie)" to add cookies to the jsp page. In your case when the user logs in or visits the jsp page it check whether he's new user or he's a returning user.
Lets think he's a first time visitor then the code will create a cookie and uses the addCookie() method to set the cookie.
A cookie is a small text file which will be stored on client browsers private place. It'll have name value combination. When the user tries to visit the page using the browser the browser sends the cookies attached to that page using the request object.
At the server side you can access them as Cookie object instaces. To get the cookies we use request.getCookies() method which will return an array of cookies attached to this request object [to the requested jsp page].
In your code you can see the getName() method which will return the name of the cookie or the name part of the name value pair in a cookie. And you are comparing it with "NameCookie" string, which means if a cookie is already created with the name "NameCookie"!. the you've used getValue() method to get the value of the cookie ie. user name in your case. As you've got the cookie which means user is visiting it for the second time you've set the cookieExists variable value to "true".

Hope its usefull to you.

Regards,
Rakesh





Similar Threads
Thread Thread Starter Forum Replies Last Post
Can someone explain this code to me pandu345 Java Basics 2 October 6th, 2006 12:25 PM
could any one explain this code for me ? method Access VBA 1 August 13th, 2005 02:02 AM
explain please rein Javascript 3 January 31st, 2005 09:47 AM
Could somebody explain this to me, please? czambran BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 3 June 7th, 2004 04:05 PM
could any one explain this query to me somanchivasu Access 3 April 6th, 2004 08:23 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.