Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture 9.2.1 Interface Servlet and.

Similar presentations


Presentation on theme: " 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture 9.2.1 Interface Servlet and."— Presentation transcript:

1  2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture 9.2.1 Interface Servlet and the Servlet Life Cycle 9.2.2 HttpServlet Class 9.2.3 HttpServletRequest Interface 9.2.4 HttpServletResponse Interface 9.3 Handling HTTP get Requests 9.4 Handling HTTP get Requests Containing Data 9.5 Handling HTTP post Requests

2  2002 Prentice Hall. All rights reserved. 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  2002 Prentice Hall. All rights reserved. 9.1 Introduction (Cont.) Servlets –Thin clients –Request/response mechanism –Session-tracking capabilities –redirection

4  2002 Prentice Hall. All rights reserved. 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

5  2002 Prentice Hall. All rights reserved. 9.2.1 Interface Servlet and the Servlet Life Cycle (Cont.)

6  2002 Prentice Hall. All rights reserved. 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

7  2002 Prentice Hall. All rights reserved. 9.2.2 HttpServlet Class (Cont.)

8  2002 Prentice Hall. All rights reserved. 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

9  2002 Prentice Hall. All rights reserved. 9.2.3 HttpServletRequest Interface (Cont.)

10  2002 Prentice Hall. All rights reserved. 9.2.4 HttpServletResponse Interface Web server –creates an HttpServletResponse object –passes it to the servlet’s service method

11  2002 Prentice Hall. All rights reserved. 9.2.4 HttpServletResponse Interface (Cont.)

12  2002 Prentice Hall. All rights reserved. 9.3 Handling HTTP get Requests get request –Retrieve the content of a URL Example: WelcomeServlet –a servlet handles HTTP get requests

13  2002 Prentice Hall. All rights reserved. Outline Fig. 9.5 WelcomeServlet that responds to a simple HTTP get request. Lines 5-6 Line 9 Lines 12-44 Line 16 Line 17 Lines 22-42 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( " " ); 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 " " ); 30 31 // head section of document 32 out.println( " " ); 33 out.println( " A Simple Servlet Example " ); 34 out.println( " " ); 35 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 setContentType method to specify the content type of the data to be sent as the response to the client. 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. Create the XHTML document by writing strings with the out object’s println method.

14  2002 Prentice Hall. All rights reserved. Outline Fig. 9.5 WelcomeServlet that responds to a simple HTTP get request. Line 43 36 // body section of document 37 out.println( " " ); 38 out.println( " Welcome to Servlets! " ); 39 out.println( " " ); 40 41 // end XHTML document 42 out.println( " " ); 43 out.close(); // close stream to complete the page 44 } 45 } Closes the output stream, flushes the output buffer and sends the information to the client.

15  2002 Prentice Hall. All rights reserved. Outline Fig. 9.6 HTML document in which the form’s action invokes WelcomeServlet through the alias welcome1 specified in web.xml. 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 6 7 8 9 Handling an HTTP Get Request 10 11 12 13 14 15 Click the button to invoke the servlet 16 17 18 19 20 21

16  2002 Prentice Hall. All rights reserved. Outline Fig. 9.6 HTML document in which the form’s action invokes WelcomeServlet through the alias welcome1 specified in web.xml. Program output

17  2002 Prentice Hall. All rights reserved. 9.4 Handling HTTP get Requests Containing Data Servlet WelcomeServlet2 –Responds to a get request that contains data

18  2002 Prentice Hall. All rights reserved. Outline Fig. 9.11 WelcomeServlet2 responds to a get request that contains data. Line 16 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( " " ); 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 " " ); 32 The request object’s getParameter method receives the parameter name and returns the corresponding String value.

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

20  2002 Prentice Hall. All rights reserved. Outline Fig. 9.12 HTML document in which the form’s action invokes WelcomeServlet2 through the alias welcome2 specified in web.xml. Line 17 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 6 7 8 9 Processing get requests with data 10 11 12 13 14 15 16 Type your first name and press the Submit button 17 18 19 20 21 22 23 Get the first name from the user.

21  2002 Prentice Hall. All rights reserved. Outline Fig. 9.12 HTML document in which the form’s action invokes WelcomeServlet2 through the alias welcome2 specified in web.xml. Program output

22  2002 Prentice Hall. All rights reserved. 9.4 Handling HTTP get Requests Containing Data (Cont.)

23  2002 Prentice Hall. All rights reserved. 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

24  2002 Prentice Hall. All rights reserved. Outline Fig. 9.14 WelcomeServlet3 responds to a post request that contains data. Lines 12-48 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( " " ); 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 " " ); 32 Define a doPost method to responds to post requests.

25  2002 Prentice Hall. All rights reserved. Outline WelcomeServlet3. java 33 // head section of document 34 out.println( " " ); 35 out.println( 36 " Processing post requests with data " ); 37 out.println( " " ); 38 39 // body section of document 40 out.println( " " ); 41 out.println( " Hello " + firstName + ", " ); 42 out.println( "Welcome to Servlets! " ); 43 out.println( " " ); 44 45 // end XHTML document 46 out.println( " " ); 47 out.close(); // close stream to complete the page 48 } 49 }

26  2002 Prentice Hall. All rights reserved. Outline Fig. 9.15 HTML document in which the form’s action invokes WelcomeServlet3 through the alias welcome3 specified in web.xml. Lines 13-21 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 6 7 8 9 Handling an HTTP Post Request with Data 10 11 12 13 14 15 16 Type your first name and press the Submit button 17 18 19 20 21 22 23 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.

27  2002 Prentice Hall. All rights reserved. Outline Fig. 9.15 HTML document in which the form’s action invokes WelcomeServlet3 through the alias welcome3 specified in web.xml. Program output

28  2002 Prentice Hall. All rights reserved. 9.5 Handling HTTP post Requests (Cont.)


Download ppt " 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture 9.2.1 Interface Servlet and."

Similar presentations


Ads by Google