// Fig. 9.21: CookieServlet.java // Using cookies to store data on the client computer. package com.deitel.advjhtp1.servlets; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class CookieServlet extends HttpServlet { private final Map books = new HashMap(); // initialize Map books public void init() { books.put( "C", "0130895725" ); books.put( "C++", "0130895717" ); books.put( "Java", "0130125075" ); books.put( "VB6", "0134569555" ); } // receive language selection and send cookie containing // recommended book to the client protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { String language = request.getParameter( "language" ); String isbn = books.get( language ).toString(); Cookie cookie = new Cookie( language, isbn ); response.addCookie( cookie ); // must precede getWriter response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); // send XHTML page to client // start XHTML document out.println( "" ); out.println( "" ); out.println( "" ); // head section of document out.println( "" ); out.println( "Welcome to Cookies" ); out.println( "" ); // body section of document out.println( "" ); out.println( "

Welcome to Cookies! You selected " + language + "

" ); out.println( "

" + "Click here to choose another language

" ); out.println( "

" + "Click here to get book recommendations

" ); out.println( "" ); // end XHTML document out.println( "" ); out.close(); // close stream } // read cookies from client and create XHTML document // containing recommended books protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { Cookie cookies[] = request.getCookies(); // get cookies response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); // start XHTML document out.println( "" ); out.println( "" ); out.println( "" ); // head section of document out.println( "" ); out.println( "Recommendations" ); out.println( "" ); // body section of document out.println( "" ); // if there are any cookies, recommend a book for each ISBN if ( cookies != null && cookies.length != 0 ) { out.println( "

Recommendations

" ); out.println( "

" ); // get the name of each cookie for ( int i = 0; i < cookies.length; i++ ) out.println( cookies[ i ].getName() + " How to Program. ISBN#: " + cookies[ i ].getValue() + "
" ); out.println( "

" ); } else { // there were no cookies out.println( "

No Recommendations

" ); out.println( "

You did not select a language.

" ); } out.println( "" ); // end XHTML document out.println( "" ); out.close(); // close stream } } /*************************************************************** * (C) Copyright 2002 by Deitel & Associates, Inc. and * * Prentice Hall. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have * * used their best efforts in preparing the book. These * * efforts include the development, research, and testing of * * the theories and programs to determine their effectiveness. * * The authors and publisher make no warranty of any kind, * * expressed or implied, with regard to these programs or to * * the documentation contained in these books. The authors * * and publisher shall not be liable in any event for * * incidental or consequential damages in connection with, or * * arising out of, the furnishing, performance, or use of * * these programs. * ***************************************************************/