State management in JSP – 2

This entry is part 10 of 14 in the series JSP Course

So in this tutorial we are going to explore cookies and we’ll see how can they be used in JSP. We’ll take a look at:

  • Exactly what are cookies?
  • The Cookies API.

What are cookies?

Simply put, cookies are small files which are placed on your computer. Now these files can serve various purposes. You can personalize a website for your users and keep track of their preferences. So for instance, if you have a programming news site, and the user has selected Java as their preferred development language, your site is going to display news only related to Java. Furthermore, if your site lists job openings, and the user selects their city, for instance New York. Your site is going to show job openings in New York only.

So, cookies are files containing text data, which are exchanged between the server and the web browsers. When you visit a server, your web browser is going to cookies to the server, which will then be filled with information and then returned to your browser. So when you visit the site the next time, it will remember your preferences and selections.

Contents of a cookie

A cookie is basically a name/value pair.

So for instance you have one cookie:

You can create as max as 20 cookies per site per path, but that is usually more than enough for keeping track of the user preferences.

So how are Cookies used?

When your browser connects to a server, it will only send the cookies which match the server’s domain name. For example, if you connect to cnn.com, your browser isn’t going to send it bbc.com cookies.

Cookie API

The cookie class is defined in the “javax.servlet.http” package, which is imported by default in all JSP pages. So you can simply make use of the cookie class simply in your JSP page.

Coding a Cookie

So the first thing you need to do is to construct a cookie, and here is the actual constructor from the JAVA docs:

So you create a cookie, you pass in the name of the cookie and the value you want to set for it. You give any name to the cookie as long as you are consistent with the naming convention of your application, and the value can be anything, any type of data you want to store.

Sending a Cookie to the browser

So here is the coded demonstration of how to send cookies to the browser of your users:

The added comments pretty much explain the code. We set a timeout, because if we don’t set up the timeout, the cookie accumulation in your computer will drag down the speed by a lot. The timeout using the total number of seconds. In the example code above, the set calculation amounts to the total number of seconds in a year.

Reading a Cookie from the browser

Now that we’ve sent the cookies to a browser, let’s see how to read the cookies we receive from the browser.

So now that the browser has the cookie sent by our application (the one we sent previously), we want to show the user a custom page based on their preferences. We’ve started with a random favorite city, and then declared a Cookie array, which is going to contain all the cookies returned by the browser. The “if” clause is just checking if the browser has returned any cookies. Then if there are any cookies, the for loop is going through all the cookies returned to the server, and in the inner “if” clause we are just checking the returned cookies against the name we set.

Following that, when the specific cookie is found, we are extracting the value of that temporary cookie into our favCity variable. So now we have the value from the cookie and we can use it to personalize the JSP web page. We can use this favCity variable throughout our application.

Series Navigation<< State Management in JSPJSP tag library (JSTL) >>

Leave A Comment

Your email address will not be published. Required fields are marked *