Project assignment 3: implementing shopping cart via servlets
Due Friday, April 5th at 6pm in Elena's office
Intermediate report: On Tuesday, April 2nd, before 1:30pm, please submit a short note
with your solution for part 1 and with (at least partial) solution for
part 2. You may change your decisions later, only the last submission
will be graded. You can send me this part
by e-mail (cc all project members) or as a brief paper note in
In this part of the project you will write servlets which implement a
shopping cart for your web site. The web site will keep track of
user's requests via a session. You will also implement cookies to
identify the user (or, rather, the browser) even if the session has
how to compile and run your servlets
Do not use System.exit() anywhere in your servlets. This will shut down
TOMCAT (the progranm that runs servlets on birch) and you will not be able
to run any servlets until it's restarted!
Please keep in mind that this project requires more communication
between project group members than the previous ones. You cannot just
divide up the work and each do a separate part, because the
programming decisions that you make for one part affect the
implementation of another.
Your best strategy is to start on the project all together, and once
you have figured out and tested the things that you all will be using,
such as session attributes, you can each work on details of a
particular part of the assignment. When the work is mostly finished, you need
to get together again to test your site and make sure that information is passed
correctly between all the servlets.
Part 1: mapping out your site
Your site will contain several servlets. In particular, you may have
an entry servlet, several product categories, pages with extra
information, and so on. You will also have a few pages related to a
shopping cart, at least a page that displays the selected products, the page to identify the user,
and the "Thank you" page.
Please read the entire assignment carefully and write down the names
and description of all the
servlets that you need to write. Agree on the names so that you can
use them in forms (to indicate which servlet will be handling a POST
request or to make an HTTP reference to another servlet). Agree on
which servlet will be doing what. You may change your decisions later,
but it's important to write them down carefully.
Each group will be using a separate web application (a package of
servlets, images, .jar files and so on), so you don't need to worry
about naming your servlets differently from another group. All your
servlet classes will be in the same directory, so you can refer to
them just by name without the path.
Appearance of the web site
You don't need to worry too much about the page appearance, but some
style decisions might be helpful. You might want to choose background
color or image, color of links and text, and the icons that you are
using for the shopping cart and other buttons. There are web sites
where you can download graphics for free. Make sure that you are not
copying anything that's copyrighted. The only image formats that you
should use are .gif and .jpg. The images must be stored in the directory
"images" of your web application.
It is important to agree on the style early on, so that all your
servlets have similar appearance. For instance, the link to a
shopping cart may appear in the right upper corner on every page. If
one page uses icon for the link, then all pages should use the same
Part 2: implementing session attributes
A session contains attributes that store information about the
user's selections. The session is shared between servlets on the
same server. A servlet finds out the stored information by retrieving
an attribute using the getAttribute() method of a session.
Your task in this part is to decide what attribute(s) you will need to
to be able to pass the information. The information will include the
list of products that the user has selected, the attributes of these
products (such as size, color, etc.), and possibly the quantity.
You may also implement a wish list: products that the user selects,
but does not buy at the checkout.
You need to write a class or classes which implement the session
attributes. Think of methods that you will need to work easily with
the attribute: adding products to the cart, deleting products,
modifying products, check for repetitions, and so on. In the example
in class (the ice cream shop) there is a class Order, but the
attribute of session is a vector of Orders, which was handled
explicitly in the doGet() method. In larger programs it would be
more convenient to add an extra class for handling the vector itself.
I strongly recommend that you:
- Start your project assignment with parts 1 and 2.
- Start early.
- Work on these parts together so that you all are familiar with the
attributes of session and their methods and with the structure of the site.
Part 3: displaying a clickable list of products
In this part you will need to generate a page, or pages, of products
offered by your web site. Since product information is stored in the
database, and may change, the pages for the products must be generated
by getting the product information from the database and creating an
HTML page with a button for each product. When the user clicks on the
button, the product should be added to the list of user's choices
stored as an attribute of a session.
More precisely, the doGet() method of a servlet that
displays products should do the following:
The doPost() method of the servlet should do the following:
- Get the session object, get the writer for writing the
response. If the session object does not exist, a new session has to
- Connect to the database using JDBC package (as in the previous
- Get the list of products for the page by running an SQL query.
- Generate a page which contains forms (as in the Ice Cream Shop
example in class), one for each product in the result set, possibly
with some additional options (s.a. size, color, quantity, etc.).
- If the doGet() method is invoked from a post method and the user
has added a product to the shopping cart, then add the product to the
cart (by modifying the session's attribute containing the list of
In addition to the product pages, it might be a good idea to have a separate
entrance page for the web site with the site's name, logo, etc..
If you don't have such a page, then indicate
which of the product pages is the entrance page.
Retrieve the parameter of the form. Add the product to the shopping
cart (a particular product is identified by a hidden attribute of
the form). If the user has selected some options for the product, the
options should be indicated in the shopping cart entry as well.
- Call the doPost() method.
Part 4. Displaying the shopping cart.
When user clicks on the shopping cart icon (or on the link, depending
on your implementation), the control goes to the servlet which does
The checkout includes two servlets:
- Displays the list of products in the shopping cart with the
chosen options (if any) and the prices, with the total shown after the list.
The prices should be retrived from the database, since you need to get
the most recent price.
The list should be displayed as a form which
allows user to change the quantity and possibly the options. The list
also should give user an option of deleting a product from the cart.
When the user modifies the selection and clicks on a button (say,
"Modify"), the cart gets redisplayed with the new selection.
- In addition to the list of products, the cart should offer the
- proceed to checkout (see below). At this point display After that the page should be displayed that lists
the products the user has chosen and thanks the user
- delete all (the session should be invalidated).
- go back to the shopping pages.
a form where the user enters
their name. No credit card number or other information is needed, since this has been
done in the order form. Even though the applets are not yet part of the site, we will
try to integrate them into the web site later. After the user enters the name, the "Thank you"
page is displayed.
- "Thank you" page has (approximately) the following text:
Dear the name entered in the previous page!
Thank you for buying the list of products
We have more great products at our web site.
Please click here to continue shopping or come back to our site again soon!
At this point the session should be invalidated.
Part 5. Invalidating the session
The option of deleting all products from a shopping cart should be
given at least by the servlet that displays the shopping cart, but
possibly by other pages as well.
When the user selects this option, the session is invalidated,
i.e. all the attributes are erased. You use the method invalidate() of
class HttpSession to implement this.
Make sure that when the session is invalidated, no information from
the previous session is stored.
Part 6. Identifying user by a long-term cookie
Important: the name of the cookie MUST include the name of your project,
s.a. myproject.usercookie. Otherwise servlets sent to a browser by one project group may
intefere with cookies sent by another. Session cookies automatically include the path to
the servlet into their name, but your "hand-made" cookies don't!
In addition to the cookies that manage the session (which by default
exist for a half-an-hour after the session is finished), you need to
identify the user by a cookie that has a longer life time. In
particular, you need to do the following:
The age of a cookie is set by a setMaxAge(int n) method. The age is set in seconds.
setMaxAge(0) deletes the cookie.
- If the session is new, check if the user's browser has a cookie
from your site stored in the browser. If yes, then check out the
cookie's the name or user ID stored in the cookie.
In real life you might use this information to customize the web site according
to the user's preferences, to display advertisements tailored to choices of the user,
or to keep statistics. In this simple simluation just greet the user by name if the cookie
stores a name, or display the greeting "Welcome back!" if it doesn't.
Optionally, you might display the product web page that the user has visited last.
If the user does not have a cookie, send a cookie to the browser with some
randomly generated ID. The life time of the cookie should be 1 month (in real life it would be
longer, but in our case there is no point of keeping it longer than that).
On the pages when the user enters their name (such as the checkout page) you need to retrieve
the cookie, store the name of the user, and send the cookie back to the user.
You should give the user an option of deleting the cookie (s.a. "If you don't want us to remember your name,
please click here"). This message should be displayed in the "Thank you" page. If the user clicks
on the link, the cookie gets deleted.
Part 7. Testing your servlets
For all parts of the assignment please make sure
to test your servlets thoroughly. When you submit the assignment, please describe all
the tests that you performed, the results, and your conclusions. The grade for this part
is a large part of the total assignment grade!
In addition to testing your servlets as you are writing them, you might want to test
each other's work when it's in a fairly completed state.
This page has been created and is maintained by Elena Machkasova
Comments and suggestions are welcome at firstname.lastname@example.org
Spring Semester 2002