WebReference.com - 2 Ways to Implement Session Tracking (2/4) | WebReference

WebReference.com - 2 Ways to Implement Session Tracking (2/4)

To page 1current pageTo page 3To page 4
[previous] [next]

2 Ways to Implement Session Tracking

Hidden Fields In Forms

This is the simplest and easiest way to implement session tracking. I find this method extremely useful to get the work done quickly. I can explain this with the help of the example I was speaking about earlier - a cart to hold your books.

Imagine you visit a site and you are presented with a list of books with checkboxes next to each of them. You could select books and click on an Add to Cart submit button. The sample code for such a page is shown below.

Remember this is just what the code may look like and not the exact page. You should try to understand the logic rather than focus on the syntax. Also remember that these are all dynamic pages being generated using some language such as JSP.

<b>Search results for books</b>
<form method="post" action="serverprogram.jsp">
<input type="checkbox" name="bookID" value="100">Java Servlet Programming<br>
<input type="checkbox" name="bookID" value="101">Professional JSP<br>
<input type="submit" name="Submit" value="Add to Cart"><br>
</form>

Suppose a page similar to the above one was generated when the user searched for some books. The above page has only 2 search results. There is a form with 2 checkboxes, each next to the name of a book, and a submit button to add any selected books to the cart.

Now suppose the user clicks on the checkbox next to book named 'Java Servlet Programming,' and then clicks on the submit button. Note that the value of a checkbox is used in this case to store the bookID. Generally when you have many checkboxes each representing one-of-many kind of entities then the value for that checkbox differentiates between all of them. In our case since all the checkboxes represent books, each value represents a different bookID and thus a different book (one book of many-books). This is actually a programming concept you should be familiar with if you have done Web programming.

Now coming back to the point, if the user checked the checkbox next to the book named 'Java Servlet Programming' and then clicked the submit button, the contents of the form are all bundled together and sent to the server side program. In our case the program is named addcart.jsp. Now suppose at any further instant when the same user is searching for more books, then on a search result he might be presented with a page such as the one shown below. Remember that he has already selected a book previously, so that book should be present in his cart.

<b>Search results for books</b>
<form method="post" action="serverprogram.jsp">
<input type="hidden" name="bookID" value="100">
<input type="checkbox" name="bookID" value="150">Teach yourself WML Programming<br>
<input type="checkbox" name="bookID" value="160">Teach yourself C++<br>
<input type="submit" name="Submit" value="Add to Cart"><br>
</form>

Those of you who are experts in programming must have already figured out how hidden fields help in session tracking. For the rest of you who are like me and take more time to figure out what is happening, let me explain.

The new search result produced once again 2 new books. One book named 'Teach yourself WML Programming' with a bookID of 150 and another book named 'Teach Yourself C++' with a bookID of 160. So a form was generated with the names of these 2 books and with 2 checkboxes so that the user may select any of these books and add them to the cart. But there is one more important thing in the form that was generated. There is a hidden input field named bookID and having a value of 100. You might have noticed that 100 was the bookID of the book named 'Java Servlet Programming' which the user had initially selected. This line describing a hidden input does not make any difference on the HTML page displayed in the browser. It would be totally invisible to the user; but within the form it makes a lot of a difference. As the user keeps adding more and more books, there would be many hidden input fields each with a different value, each representing a previously selected book. When this form is submitted to the server side program, that program would not only fetch the newly selected checkboxes (newly selected books) but also these hidden fields each representing a previously selected book by that user. Note that all the input fields have the same name bookID but their values are different. Within the server side program you would simply expect a parameter called bookID which would be an array with different values. You could extract all the values and then use them as required. It is the job of the server side program to add these lines indicating hidden fields whenever it generates a new page.

Once again... the main concept to be understood is that a hidden field displays nothing on the HTML page. So the user who is browsing the page sees nothing unusual, but the value associated with these hidden fields can be used to hold any kind of data that you want. Care should be taken so that every time your server side program generates a new form, it should read all the parameters passed to it from the previous form and then add all these values as new hidden fields in any new form that it generates. Thus you could carry information from one HTML page to another and thus maintain a connection between 2 pages.

The disadvantage of this method of session tracking is that if you do not want the user to know what information is being passed around to maintain a session (in case that information is somewhat sensitive, maybe a password or something) then this method is not the best one since the user can simply select to View the Source of the HTML page and see all the hidden fields present in the form.


To page 1current pageTo page 3To page 4
[previous] [next]

Created: March 27, 2002
Revised: March 27, 2002

URL: http://webreference.com/programming/sessions/2.html