// Fig. 9.18: RedirectServlet.java // Redirecting a user to a different Web page. package com.deitel.advjhtp1.servlets; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class RedirectServlet extends HttpServlet { // process "get" request from client protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { String location = request.getParameter( "page" ); if ( location != null ) if ( location.equals( "deitel" ) ) response.sendRedirect( "http://www.deitel.com" ); else if ( location.equals( "welcome1" ) ) response.sendRedirect( "welcome1" ); // code that executes only if this servlet // does not redirect the user to another page 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( "Invalid page" ); out.println( "" ); // body section of document out.println( "" ); out.println( "

Invalid page requested

" ); out.println( "

" ); out.println( "Click here to choose again

" ); out.println( "" ); // end XHTML document out.println( "" ); out.close(); // close stream to complete the page } } /*************************************************************** * (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. * ***************************************************************/