How do I get the Duke's Bookstore servlet example at the Java Developer Connection to work under Servlet API version 2.2 (and Tomcat 3.1)?

Avi Kak

The Duke's Bookstore example at the Java Developer Connection is indeed very instructive. As currently posted, this example works for JSDK 2.1. To get it to work under Tomcat 3.1, which uses the Servlet API 2.2, you'd need to make the following changes:

In the override definitions of the doGet(HttpServletRequest, HttpServletResponse) method in the files CatalogServlet.java, ShowCartServlet.java, Cashier.servlet, BookDetailServlet.java, and in the override definition of the doPost(HttpServletRequest, HttpServletResponse) method in the file ReceiptServlet.java, replace the invocation

      ShoppingCart cart = (ShoppingCart)session.getValue(session.getId());


      ShoppingCart cart = (ShoppingCart)session.getAttribute(session.getId());

and the invocation

      session.putValue(session.getId(), cart); 


      session.setAttribute(session.getId(), cart);

The methods getValue(String) and putValue(String, Object) declared for the HttpSession interface have been deprecated in the Servlet2.2 API and replaced by the getAttribute(String) and setAttribute(String, Object) methods.

Some of the other changes you'd need to make depend on how and where you install the code in Tomcat3.1. For example, I have installed the code in a directory called bookstore under C: omcatwebapps. I put all the servlets and the support classes in C: omcatwebappsbookstoreWEB-INFclasses and the html page bookstore.html in C: omcatwebappsbookstoreservlets. The resulting pathnames to the servlets and the html page would need to be reflected in the code.