1 / 77

Chapter 9: Servlets

Chapter 9: Servlets.

cleveland
Download Presentation

Chapter 9: Servlets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9: Servlets Outline9.1 Introduction9.2 Servlet Overview and Architecture9.2.1 Interface Servlet and the Servlet Life Cycle9.2.2 HttpServlet Class9.2.3 HttpServletRequest Interface9.2.4 HttpServletResponse Interface9.3 Handling HTTP get Requests9.3.1 Setting Up the Apache Tomcat Server9.3.2 Deploying a Web Application9.4 Handling HTTP get Requests Containing Data9.5 Handling HTTP post Requests9.6 Redirecting Requests to Other Resources9.7 Session Tracking9.7.1 Cookies9.7.2 Session Tracking with HttpSession9.8 Multi-Tier Applications: Using JDBC from a Servlet9.9 HttpUtils Class9.10 Internet and World Wide Web Resources

  2. 9.1 Introduction • Java networking capabilities • Socket-based and packet-based communications • Package java.net • Remote Method Invocation (RMI) • Package java.rmi • Servlets and Java Server Pages (JSP) • Request-response model • Packages javax.servlet javax.servlet.http javax.servlet.jsp javax.servlet.tagext • Form the Web tier of J2EE

  3. 9.1 Introduction (Cont.) • Servlets • Thin clients • Request/response mechanism • Session-tracking capabilities • redirection • Tomcat • Jakarta project • Official reference implementation of the JSP and servlet standards

  4. 9.2 Servlet Overview and Architecture • Servlet container (servlet engine) • Server that executes a servlet • Web servers and application servers • Netscape iPlanet Application Server • Microsoft’s Internet Information Server (IIS) • Apache HTTP Server • BEA’s WebLogic Application Server • IBM’s WebSphere Application Server • World Wide Web Consortium’s Jigsaw Web Server

  5. 9.2.1 Interface Servlet and the Servlet Life Cycle • Interface Servlet • All servlets must implement this interface • All methods of interface Servlet are invoked automatically • Servlet life cycle • Servlet container invokes the servlet’s init method • Servlet’s service method handles requests • Servlet’s destroy method releases servlet resources when the servlet container terminates the servlet • Servlet implementation • GenericServlet • HttpServlet

  6. 9.2.1 Interface Servlet and the Servlet Life Cycle (Cont.)

  7. 9.2.2 HttpServlet Class • Overrides method service • Two most common HTTP request types • get requests • post requests • Method doGet responds to get requests • Method doPost responds to post requests • HttpServletRequest and HttpServletResponse objects

  8. 9.2.2 HttpServlet Class (Cont.)

  9. 9.2.3 HttpServletRequest Interface • Web server • creates an HttpServletRequest object • passes it to the servlet’s service method • HttpServletRequest object contains the request from the client

  10. 9.2.3 HttpServletRequest Interface (Cont.)

  11. 9.2.4 HttpServletResponse Interface • Web server • creates an HttpServletResponse object • passes it to the servlet’s service method

  12. 9.2.4 HttpServletResponse Interface (Cont.)

  13. 9.3 Handling HTTP get Requests • get request • Retrieve the content of a URL • Example: WelcomeServlet • a servlet handles HTTP get requests

  14. Import the javax.servlet and javax.servlet.http packages. Extends HttpServlet to handle HTTP get requests and HTTP post requests. Override method doGet to provide custom get request processing. Uses the response object’s getWriter method to obtain a reference to the PrintWriter object that enables the servlet to send content to the client. Uses the response object’s setContentType method to specify the content type of the data to be sent as the response to the client. Create the XHTML document by writing strings with the out object’s println method. 1 // Fig. 9.5: WelcomeServlet.java 2 // A simple servlet to process get requests. 3 package com.deitel.advjhtp1.servlets; 4 5 import javax.servlet.*; 6 import javax.servlet.http.*; 7 import java.io.*; 8 9 public class WelcomeServlet extends HttpServlet { 10 11 // process "get" requests from clients 12 protected void doGet( HttpServletRequest request, 13 HttpServletResponse response ) 14 throws ServletException, IOException 15 { 16 response.setContentType( "text/html" ); 17 PrintWriter out = response.getWriter(); 18 19 // send XHTML page to client 20 21 // start XHTML document 22 out.println( "<?xml version = \"1.0\"?>" ); 23 24 out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " + 25 "XHTML 1.0 Strict//EN\" \"http://www.w3.org" + 26 "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" ); 27 28 out.println( 29 "<html xmlns = \"http://www.w3.org/1999/xhtml\">" ); 30 31 // head section of document 32 out.println( "<head>" ); 33 out.println( "<title>A Simple Servlet Example</title>" ); 34 out.println( "</head>" ); 35 Fig. 9.5 WelcomeServlet that responds to a simple HTTP get request.Lines 5-6Line 9Lines 12-44Line 16Line 17Lines 22-42

  15. Closes the output stream, flushes the output buffer and sends the information to the client. 36 // body section of document 37 out.println( "<body>" ); 38 out.println( "<h1>Welcome to Servlets!</h1>" ); 39 out.println( "</body>" ); 40 41 // end XHTML document 42 out.println( "</html>" ); 43 out.close(); // close stream to complete the page 44 } 45 } Fig. 9.5 WelcomeServlet that responds to a simple HTTP get request.Line 43

  16. 1 <?xml version ="1.0"?> 2 <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Fig. 9.6: WelcomeServlet.html --> 6 7 <html xmlns = "http://www.w3.org/1999/xhtml"> 8 <head> 9 <title>Handling an HTTP Get Request</title> 10 </head> 11 12 <body> 13 <form action ="/advjhtp1/welcome1" method ="get"> 14 15 <p><label>Click the button to invoke the servlet 16 <input type = "submit"value ="Get HTML Document"/> 17 </label></p> 18 19 </form> 20 </body> 21 </html> Fig. 9.6 HTML document in which the form’s action invokes WelcomeServlet through the alias welcome1 specified in web.xml.

  17. Fig. 9.6 HTML document in which the form’s action invokes WelcomeServlet through the alias welcome1 specified in web.xml.Program output

  18. 9.3.1 Setting Up the Apache Tomcat Server • Download Tomcat (version 3.2.3) • Jakarta.apache.org/builds/jakarta-tomcat/release/v3.2.3/bin/ • Define environment variables • JAVA_HOME • TOMCAT_HOME • Start the Tomcat server • tomcat start • Launch the Tomcat server • http://localhost:8080/

  19. 9.3.1 Setting Up the Apache Tomcat Server (Cont.). Fig. 9.7 Tomcat documentation home page. (Courtesy of The Apache Software Foundation.)

  20. 9.3.2 Deploying a Web Application • Web applications • JSPs, servlets and their supporting files • Deploying a Web application • Directory structure • Context root • Web application archive file (WAR file) • Deployment descriptor • web.xml

  21. 9.3.2 Deploying a Web Application (Cont.)

  22. Element web-app defines the configuration of each servlet in the Web application and the servlet mapping for each servlet. Element display-name specifies a name that can be displayed to the administrator of the server on which the Web application is installed. Element description specifies a description of the Web application that might be displayed to the administrator of the server. Element servlet-name is the name for the servlet. Element description specifies a description for this particular servlet. Element servlet describes a servlet. Element servlet-class specifies compiled servlet’s fully qualified class name. 1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" 3 "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> 4 5 <web-app> 6 7 <!-- General description of your Web application --> 8 <display-name> 9 Advanced Java How to Program JSP 10 and Servlet Chapter Examples 11 </display-name> 12 13 <description> 14 This is the Web application in which we 15 demonstrate our JSP and Servlet examples. 16 </description> 17 18 <!-- Servlet definitions --> 19 <servlet> 20 <servlet-name>welcome1</servlet-name> 21 22 <description> 23 A simple servlet that handles an HTTP get request. 24 </description> 25 26 <servlet-class> 27 com.deitel.advjhtp1.servlets.WelcomeServlet 28 </servlet-class> 29 </servlet> 30 Fig. 9.9 Deployment descriptor (web.xml) for the advjhtp1 Web application.Lines 5-37Lines 8-11Lines 13-16Lines 19-29Line 20 Lines 22-24Lines 26-28

  23. Element servlet-mapping specifies servlet-name and url-pattern elements. 31 <!-- Servlet mappings --> 32 <servlet-mapping> 33 <servlet-name>welcome1</servlet-name> 34 <url-pattern>/welcome1</url-pattern> 35 </servlet-mapping> 36 37 </web-app> Fig. 9.9 Deployment descriptor (web.xml) for the advjhtp1 Web application.Lines 32-35

  24. 9.3.2 Deploying a Web Application (Cont.) • Invoke WelcomeServlet example • /advjhtp1/welcome1 • /advjhtp1 specifies the context root • /welcome1 specifies the URL pattern • URL pattern formats • Exact match • /advjhtp1/welcome1 • Path mappings • /advjhtp1/example/* • Extension mappings • *.jsp • Default servlet • /advjhtp1/example/

  25. 9.3.2 Deploying a Web Application (Cont.)

  26. 9.4 Handling HTTP get Requests Containing Data • Servlet WelcomeServlet2 • Responds to a get request that contains data

  27. The request object’s getParameter method receives the parameter name and returns the corresponding String value. 1 // Fig. 9.11: WelcomeServlet2.java 2 // Processing HTTP get requests containing data. 3 package com.deitel.advjhtp1.servlets; 4 5 import javax.servlet.*; 6 import javax.servlet.http.*; 7 import java.io.*; 8 9 public class WelcomeServlet2 extends HttpServlet { 10 11 // process "get" request from client 12 protected void doGet( HttpServletRequest request, 13 HttpServletResponse response ) 14 throws ServletException, IOException 15 { 16 String firstName = request.getParameter( "firstname" ); 17 18 response.setContentType( "text/html" ); 19 PrintWriter out = response.getWriter(); 20 21 // send XHTML document to client 22 23 // start XHTML document 24 out.println( "<?xml version = \"1.0\"?>" ); 25 26 out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " + 27 "XHTML 1.0 Strict//EN\" \"http://www.w3.org" + 28 "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" ); 29 30 out.println( 31 "<html xmlns = \"http://www.w3.org/1999/xhtml\">" ); 32 Fig. 9.11 WelcomeServlet2 responds to a get request that contains data.Line 16

  28. Uses the result of line 16 as part of the response to the client. 33 // head section of document 34 out.println( "<head>" ); 35 out.println( 36 "<title>Processing get requests with data</title>" ); 37 out.println( "</head>" ); 38 39 // body section of document 40 out.println( "<body>" ); 41 out.println( "<h1>Hello " + firstName + ",<br />" ); 42 out.println( "Welcome to Servlets!</h1>" ); 43 out.println( "</body>" ); 44 45 // end XHTML document 46 out.println( "</html>" ); 47 out.close(); // close stream to complete the page 48 } 49 } Fig. 9.11 WelcomeServlet2 responds to a get request that contains data.Line 41

  29. Get the first name from the user. 1 <?xml version ="1.0"?> 2 <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Fig. 9.12: WelcomeServlet2.html --> 6 7 <html xmlns ="http://www.w3.org/1999/xhtml"> 8 <head> 9 <title>Processing get requests with data</title> 10 </head> 11 12 <body> 13 <form action ="/advjhtp1/welcome2"method ="get"> 14 15 <p><label> 16 Type your first name and press the Submit button 17 <br /><input type ="text"name = "firstname"/> 18 <input type = "submit"value ="Submit" /> 19 </p></label> 20 21 </form> 22 </body> 23 </html> Fig. 9.12 HTML document in which the form’s action invokes WelcomeServlet2 through the alias welcome2 specified in web.xml.Line 17

  30. Fig. 9.12 HTML document in which the form’s action invokes WelcomeServlet2 through the alias welcome2 specified in web.xml. Program output

  31. 9.4 Handling HTTP get Requests Containing Data (Cont.)

  32. 9.5 Handling HTTP post Requests • HTTP post request • Post data from an HTML form to a server-side form handler • Browsers cache Web pages • Servlet WelcomeServlet3 • Responds to a post request that contains data

  33. Define a doPost method to responds to post requests. 1 // Fig. 9.14: WelcomeServlet3.java 2 // Processing post requests containing data. 3 package com.deitel.advjhtp1.servlets; 4 5 import javax.servlet.*; 6 import javax.servlet.http.*; 7 import java.io.*; 8 9 public class WelcomeServlet3 extends HttpServlet { 10 11 // process "post" request from client 12 protected void doPost( HttpServletRequest request, 13 HttpServletResponse response ) 14 throws ServletException, IOException 15 { 16 String firstName = request.getParameter( "firstname" ); 17 18 response.setContentType( "text/html" ); 19 PrintWriter out = response.getWriter(); 20 21 // send XHTML page to client 22 23 // start XHTML document 24 out.println( "<?xml version = \"1.0\"?>" ); 25 26 out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " + 27 "XHTML 1.0 Strict//EN\" \"http://www.w3.org" + 28 "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" ); 29 30 out.println( 31 "<html xmlns = \"http://www.w3.org/1999/xhtml\">" ); 32 Fig. 9.14 WelcomeServlet3 responds to a post request that contains data.Lines 12-48

  34. 33 // head section of document 34 out.println( "<head>" ); 35 out.println( 36 "<title>Processing post requests with data</title>" ); 37 out.println( "</head>" ); 38 39 // body section of document 40 out.println( "<body>" ); 41 out.println( "<h1>Hello " + firstName + ",<br />" ); 42 out.println( "Welcome to Servlets!</h1>" ); 43 out.println( "</body>" ); 44 45 // end XHTML document 46 out.println( "</html>" ); 47 out.close(); // close stream to complete the page 48 } 49 } WelcomeServlet3.java

  35. Provide a form in which the user can input a name in the text input element firstname, then click the Submit button to invoke WelcomeServlet3. 1 <?xml version ="1.0"?> 2 <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Fig. 9.15: WelcomeServlet3.html --> 6 7 <html xmlns ="http://www.w3.org/1999/xhtml"> 8 <head> 9 <title>Handling an HTTP Post Request with Data</title> 10 </head> 11 12 <body> 13 <form action = "/advjhtp1/welcome3"method = "post"> 14 15 <p><label> 16 Type your first name and press the Submit button 17 <br /><input type ="text"name = "firstname" /> 18 <input type ="submit"value = "Submit" /> 19 </label></p> 20 21 </form> 22 </body> 23 </html> Fig. 9.15 HTML document in which the form’s action invokes WelcomeServlet3 through the alias welcome3 specified in web.xml.Lines 13-21

  36. Fig. 9.15 HTML document in which the form’s action invokes WelcomeServlet3 through the alias welcome3 specified in web.xml.Program output

  37. 9.5 Handling HTTP post Requests (Cont.)

  38. 9.6 Redirecting Requests to Other Resources • Servlet RedirectServlet • Redirects the request to a different resource

  39. Obtains the page parameter from the request. Determine if the value is either “deitel” or “welcome1” Redirects the request to www.deitel.com. Redirects the request to the servlet WelcomeServlet. Output a Web page indicating that an invalid request was made if method sendRedirect is not called. 1 // Fig. 9.17: RedirectServlet.java 2 // Redirecting a user to a different Web page. 3 package com.deitel.advjhtp1.servlets; 4 5 import javax.servlet.*; 6 import javax.servlet.http.*; 7 import java.io.*; 8 9 public class RedirectServlet extends HttpServlet { 10 11 // process "get" request from client 12 protected void doGet( HttpServletRequest request, 13 HttpServletResponse response ) 14 throws ServletException, IOException 15 { 16 String location = request.getParameter( "page" ); 17 18 if ( location != null ) 19 20 if ( location.equals( "deitel" ) ) 21 response.sendRedirect( "http://www.deitel.com" ); 22 else 23 if ( location.equals( "welcome1" ) ) 24 response.sendRedirect( "welcome1" ); 25 26 // code that executes only if this servlet 27 // does not redirect the user to another page 28 29 response.setContentType( "text/html" ); 30 PrintWriter out = response.getWriter(); 31 Fig. 9.17 Redirecting requests to other resources.Line 16Lines 20-24Line 21Line 24Lines 29-56

  40. 32 // start XHTML document 33 out.println( "<?xml version = \"1.0\"?>" ); 34 35 out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " + 36 "XHTML 1.0 Strict//EN\" \"http://www.w3.org" + 37 "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" ); 38 39 out.println( 40 "<html xmlns = \"http://www.w3.org/1999/xhtml\">" ); 41 42 // head section of document 43 out.println( "<head>" ); 44 out.println( "<title>Invalid page</title>" ); 45 out.println( "</head>" ); 46 47 // body section of document 48 out.println( "<body>" ); 49 out.println( "<h1>Invalid page requested</h1>" ); 50 out.println( "<p><a href = " + 51 "\"servlets/RedirectServlet.html\">" ); 52 out.println( "Click here to choose again</a></p>" ); 53 out.println( "</body>" ); 54 55 // end XHTML document 56 out.println( "</html>" ); 57 out.close(); // close stream to complete the page 58 } 59 } Fig. 9.17 Redirecting requests to other resources.

  41. Provide hyperlinks that allow the user to invoke the servlet RedirectServlet. 1 <?xml version ="1.0"?> 2 <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Fig. 9.18: RedirectServlet.html --> 6 7 <html xmlns = "http://www.w3.org/1999/xhtml"> 8 <head> 9 <title>Redirecting a Request to Another Site</title> 10 </head> 11 12 <body> 13 <p>Click a link to be redirected to the appropriate page</p> 14 <p> 15 <a href = "/advjhtp1/redirect?page=deitel"> 16 www.deitel.com</a><br /> 17 <a href = "/advjhtp1/redirect?page=welcome1"> 18 Welcome servlet</a> 19 </p> 20 </body> 21 </html> Fig. 9.18 RedirectServlet.html document to demonstrate redirecting requests to other resources.Lines 15-16Lines 17-18

  42. Fig. 9.18 RedirectServlet.html document to demonstrate redirecting requests to other resources.Program output

  43. 9.6 Redirecting Requests to other Resources (Cont.)

  44. 9.7 Session Tracking • Personalization • Privacy invasion • HTTP – stateless protocol • Does not support persistent information • Track clients individually • Cookies • Session tracking • hidden type input • URL rewriting

  45. 9.7.1 Cookies • Stored on the user’s computer for retrieval later • Text-based data sent by servlets • Maximum age of a cookie • Deleted automatically when they expire • Servlet CookieServlet • Handles both get and post requests

  46. Method init populates books with four key/value pair of books. Uses method getParameter to obtain the user’s language selection. Gets the ISBN number for the selected language from books. Creates a new Cookie object, using the language and isbn values as the cookie name and cookie value, respectively. Adds the cookie to the response with method addCookie of interface HttpServletResponse. 1 // Fig. 9.20: CookieServlet.java 2 // Using cookies to store data on the client computer. 3 package com.deitel.advjhtp1.servlets; 4 5 import javax.servlet.*; 6 import javax.servlet.http.*; 7 import java.io.*; 8 import java.util.*; 9 10 public class CookieServlet extends HttpServlet { 11 private final Map books = new HashMap(); 12 13 // initialize Map books 14 public void init() 15 { 16 books.put( "C","0130895725" ); 17 books.put( "C++", "0130895717" ); 18 books.put( "Java", "0130125075" ); 19 books.put( "VB6", "0134569555" ); 20 } 21 22 // receive language selection and send cookie containing 23 // recommended book to the client 24 protected void doPost( HttpServletRequest request, 25 HttpServletResponse response ) 26 throws ServletException, IOException 27 { 28 String language = request.getParameter( "language" ); 29 String isbn = books.get( language ).toString(); 30 Cookie cookie = new Cookie( language, isbn ); 31 32 response.addCookie( cookie ); // must precede getWriter 33 response.setContentType( "text/html" ); 34 PrintWriter out = response.getWriter(); 35 Fig. 9.20 Storing user data on the client computer with cookies.Lines 14-20Line 28Line 29 Line 30Line 32

  47. 36 // send XHTML page to client 37 38 // start XHTML document 39 out.println( "<?xml version = \"1.0\"?>" ); 40 41 out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " + 42 "XHTML 1.0 Strict//EN\" \"http://www.w3.org" + 43 "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" ); 44 45 out.println( 46 "<html xmlns = \"http://www.w3.org/1999/xhtml\">" ); 47 48 // head section of document 49 out.println( "<head>" ); 50 out.println( "<title>Welcome to Cookies</title>" ); 51 out.println( "</head>" ); 52 53 // body section of document 54 out.println( "<body>" ); 55 out.println( "<p>Welcome to Cookies! You selected " + 56 language + "</p>" ); 57 58 out.println( "<p><a href = " + 59 "\"/advjhtp1/servlets/CookieSelectLanguage.html\">" + 60 "Click here to choose another language</a></p>" ); 61 62 out.println( "<p><a href = \"/advjhtp1/cookies\">" + 63 "Click here to get book recommendations</a></p>" ); 64 out.println( "</body>" ); 65 66 // end XHTML document 67 out.println( "</html>" ); 68 out.close(); // close stream 69 } 70 Fig. 9.20 Storing user data on the client computer with cookies.

  48. Retrieves the cookies from the client using HttpServletRequest method getCookies, which returns an array of Cookie objects. 71 // read cookies from client and create XHTML document 72 // containing recommended books 73 protected void doGet( HttpServletRequest request, 74 HttpServletResponse response ) 75 throws ServletException, IOException 76 { 77 Cookie cookies[] = request.getCookies(); // get cookies 78 79 response.setContentType( "text/html" ); 80 PrintWriter out = response.getWriter(); 81 82 // start XHTML document 83 out.println( "<?xml version = \"1.0\"?>" ); 84 85 out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " + 86 "XHTML 1.0 Strict//EN\" \"http://www.w3.org" + 87 "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" ); 88 89 out.println( 90 "<html xmlns = \"http://www.w3.org/1999/xhtml\">" ); 91 92 // head section of document 93 out.println( "<head>" ); 94 out.println( "<title>Recommendations</title>" ); 95 out.println( "</head>" ); 96 97 // body section of document 98 out.println( "<body>" ); 99 100 // if there are any cookies, recommend a book for each ISBN 101 if ( cookies != null && cookies.length != 0 ) { 102 out.println( "<h1>Recommendations</h1>" ); 103 out.println( "<p>" ); 104 Fig. 9.20 Storing user data on the client computer with cookies.Line 77

  49. 105 // get the name of each cookie 106 for ( int i = 0; i < cookies.length; i++ ) 107 out.println( cookies[ i ].getName() + 108 " How to Program. ISBN#: " + 109 cookies[ i ].getValue() + "<br />" ); 110 111 out.println( "</p>" ); 112 } 113 else { // there were no cookies 114 out.println( "<h1>No Recommendations</h1>" ); 115 out.println( "<p>You did not select a language.</p>" ); 116 } 117 118 out.println( "</body>" ); 119 120 // end XHTML document 121 out.println( "</html>" ); 122 out.close(); // close stream 123 } 124 } Fig. 9.20 Storing user data on the client computer with cookies.

  50. 1 <?xml version ="1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Fig. 9.21: CookieSelectLanguage.html --> 6 7 <html xmlns = "http://www.w3.org/1999/xhtml"> 8 <head> 9 <title>Using Cookies</title> 10 </head> 11 12 <body> 13 <form action = "/advjhtp1/cookies"method ="post"> 14 15 <p>Select a programming language:</p> 16 <p> 17 <input type ="radio"name ="language" 18 value ="C"/>C <br /> 19 20 <input type = "radio"name ="language" 21 value ="C++" />C++ <br /> 22 23 <!-- this radio button checked by default --> 24 <input type = "radio"name ="language" 25 value = "Java" checked ="checked"/>Java<br /> 26 27 <input type = "radio" name ="language" 28 value ="VB6"/>VB 6 29 </p> 30 31 <p><input type ="submit"value = "Submit"/></p> 32 33 </form> 34 </body> 35 </html> Fig. 9.21 CookieSelectLanguage.html document for selecting a programming language and posting the data to the CookieServlet.

More Related