Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next

The JSP Files - Part 6: State of Grace

A Perfect State

Over the past few weeks, you've learned a great deal about the various control structures and objects available in JSP. You've see how to retrieve information posted in an online form, and connect your JSP document to a database for dynamic content generation.

This week in The JSP Files, we're going to tackle yet another very interesting topic - the problem of maintaining "state" on a Web site. We'll be looking at two common solutions to this problem - cookies and server-based sessions - and using simple examples to illustrate the JSP constructs available to help you identify and track client requests on your Web site.

You'll also learn more than you want to know about what exactly "maintaining state" actually means, the advantages and disadvantages of each of the approaches just described...and, if we're doing our job right, get a laugh or two out of the whole exercise.

Wasted, Dude!

It's one of the things geeks say to each other when they want to impress the young women in earshot: "HTTP is a stateless protocol, and the Internet is a stateless development environment". In simple language, all this means is that the HyperText Transfer Protocol, which is the backbone of the Web, is unable to retain a memory of the identity of each client that connects to a Web site, and therefore treats each request for a Web page as a unique and independent connection, with no relationship whatsoever to the connections that preceded it - very similar to the behaviour of some of today's more adventurous teenagers, who get drunk every night, wake up the next morning with no memory at all of what happened, and go out again in the evening to do the same thing all over again...

Now, so long as you're aimlessly surfing from one site to another, this works without a problem. But what if you've decided to buy a few discs from CDNow.com? In a "stateless environment", it would be very difficult to keep track of all the items you've shortlisted for purchase, as the stateless nature of the HTTP protocol would make it impossible to keep track of the items selected.

Consequently, what is required is a method that makes it possible to "maintain state", something that allows client connections to be tracked and connection-specific data to be maintained. And thus came about "cookies", which allowed Web sites to store client-specific information in a file on the client system, and access the information in the file whenever required. So, in the shopping cart example above, the items selected would be added to the cookie, and would be retrieved and presented to the customer in a consolidated list during the billing process.

Why are they called "cookies"? The PR agency handling the account was obviously asleep at the wheel.

A Few Ground Rules

Since cookies are used to record information about your activities on a particular site, they can only be read by the site that created them. For example, Yahoo and Deja.com store your username in a cookie on your hard drive and use this information to automatically fill in log-in forms the next time you visit their Web sites. It's kinda like going to a chic restaurant, and having the maitre'd call you by name (something which hasn't happened to us of late!)

Before getting into the nitty-gritty of cookie technology, a few ground rules are in order:

  1. A single domain cannot set more than twenty cookies. A single cookie cannot exceed 4 KB in size. The maximum number of cookies that may be set is 300.

  2. The most common method of transmitting a cookie to a client is via the "Set-Cookie" HTTP header.

  3. A cookie usually possesses five types of attributes.

    The first of these is a NAME=VALUE pair, used to store information such as a username, email address or credit-card number. The NAME is a string used to identify the cookie, while the VALUE is the data to be stored in the cookie. For example,

    clarkkent=superman

    The EXPIRES attribute defines the date on which the cookie is automatically removed from the system. The date must be in the format "weekday, dd-mon-yy hh:mm:ss GMT". For example,

    expires="Sun, 31-Dec-2030 17:51:06 GMT"

    Cookies without a specifically defined expiry date remain active for so long as the browser remains open, and are destroyed once the browser is closed. You can delete an existing cookie be setting this attribute to a date in the past.

    The PATH attribute is used to set the top-level directory on the Web server from which cookies can be accessed. In most cases, this is set to

    path=/

    to ensure that the cookie can be accessed by each and every document on the server.

    The DOMAIN attribute is used to specify the domain which the cookie is linked to, and the SECURE attribute indicates that a cookie should only be set if there exists a secure protocol between the browser and the server.

  4. Of all the five attributes, the first is the only one that is not optional.

  5. Every good browser offers users the option to disable cookies. If a user decides to exercise his or her right to do so, your cookies will not be stored, and any attempt to access them will fail. Users who do this are usually career criminals or tax evaders.

Copyright Melonfire, 2000. All rights reserved.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links