This is the simplest and most easy 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 - A Cart to hold your books.
In case you visit a site and you are presented a list of books with checkboxes
next to each of them. You could select books and click on a Add to Cart Submit
button. A 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 entity 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 would be familiar with in case you have done web programming.
Now coming back to the point, in case 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 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 and now
he would like to add more books.
<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'll who are experts in programming must have already figured out
how hidden fields help in session tracking. For the rest of you'll 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 hell lot of a difference. This way when 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. Only care is to 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 session tracking is that in case you do not want the user
to know what information is being passed around to maintain a session (in case
that information is somewhat vital..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 get to see all the hidden fields present in the Form.
Comments