Distributed Web Systems Laboratory exercises Using session tracking and working with cookies Learning goals: Learn to use servlet session tracking to store the servlet state between HTTP requests Learn to set and access cookies. Understand differences between persistent and session cookies. What to do: Create a new Ant project directory for this lab with the “src” and “web” sub- directories. Put the Ant build files from the course web page into this project directory. Write a servlet that performs the following functions (remember to use your own or provided helper classes, such as “ServletUtilities”, to speed up the development): a. Reads two input parameters submitted by the user: “product” and “quantity”. b. Obtains the contents of the customer’s shopping cart from the servlet session. c. Adds the given quantity of the specified product to the shopping cart. d. Displays a “thank you for shopping” page with the shopping cart contents to the user. e. Updates the shopping cart contents in the servlet session. A simple and sufficient for this lab way to store the shopping cart contents is an HTML-formatted text string. When the cart is empty, the string equals to “”. When we add a new product to the cart, we simply append the following text to the string: “\n<BR> product_name Quantity: product_quantity”. In this case, if we insert our cart string into an HTML document, we will get the following in the browser: product_name1 Quantity: product_quantity1 product_name2 Quantity: product_quantity2 ... product_nameN Quantity: product_quantityN Basically, we get something looking like a receipt. Of course, you can and are encouraged to use fancier and nicer looking HTML formatting here. Remember to check the correctness of the input data. Remember to place all your .java sources into the “src” sub-directory, so that Ant could compile them automatically. Distributed Web Systems Create a web application deployment descriptor for your application and put it into “web/WEB-INF”. You can modify the descriptor from the previous lab or the one available on the course web page. Choose some appropriate URL pattern for your servlet, e.g. “/AddToCart”. Create an HTML page for a shopping catalogue. The page should list a set of products and for each product provide a text input field to enter the quantity and an “Add to cart” button. To achieve this, you should create a separate form in your page for each product. Each such form should have an input text field named “quantity” for entering the amounts, and a hidden field named “product”. The value of this hidden field should be set to the product name. This way you can pass the product name to your servlet without cluttering the visible text on the page. See the example HTML fragment below and an HTML page in the illustration (hopefully, your catalogues will look much prettier!) <H2>Laptop computer. Price $3000.</H2><P> Very powerful yet light and slim. <FORM ACTION="AddToCart"> <INPUT TYPE="HIDDEN" NAME="product" VALUE="Laptop computer."><BR> Quantity: <INPUT TYPE="TEXT" NAME="quantity"> <INPUT TYPE="SUBMIT" VALUE="Add to cart"> </FORM> For the form action, you should use a URL matching the URL pattern for your “AddToCart” servlet (whatever you used in your “web.xml”). Put this HTML page into the “web” sub-directory and name it as “index.html”, so that it becomes the default page for your web application. Compile and deploy your application using Ant. Verify the functioning of your servlet. Try to populate your shopping cart, then restart the browser and see how this affected the shopping cart contents. Explain such behaviour. So far so good! Now you can use servlet sessions to keep your application state between HTTP requests. Let’s focus some more on cookies now. Distributed Web Systems Write a servlet to do the following: a. Create two cookies: one session cookie, and one more persistent cookie that should be stored for at least an hour. Name these cookies appropriately and assign some test values to them. b. Request the browser to store those cookies; c. Obtain the list of all currently active cookies and print their names and values; If your lab time is running out, you may use the ready code for this lab from the course web page. However, do spend time in this case familiarising yourself with that code. Add this servlet to your application descriptor and create an appropriate URL mapping for it there. Re-deploy your application. That is, do “ant remove” followed by “ant install”. Remember that all Ant commands must be issued in your project directory (where the Ant build files are). Call the servlet two times and explain its output. Now restart your browser and again call your servlet two times. Explain the obtained results. You are done! Remove your application.