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 Interface Servlet and the Servlet Life Cycle HttpServlet Class HttpServletRequest Interface 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 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 Interface Servlet and the Servlet Life Cycle (Cont.)

6  2002 Prentice Hall. All rights reserved 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 HttpServlet Class (Cont.)

8  2002 Prentice Hall. All rights reserved 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 HttpServletRequest Interface (Cont.)

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

11  2002 Prentice Hall. All rights reserved 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 Line 16 Line 17 Lines // 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 { // 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(); // send XHTML page to client // start XHTML document 22 out.println( " " ); out.println( "" ); out.println( 29 " " ); // 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 // body section of document 37 out.println( " " ); 38 out.println( " Welcome to Servlets! " ); 39 out.println( " " ); // 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 Handling an HTTP Get Request Click the button to invoke the servlet

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 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 { // 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" ); response.setContentType( "text/html" ); 19 PrintWriter out = response.getWriter(); // send XHTML document to client // start XHTML document 24 out.println( " " ); out.println( "" ); 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 WelcomeServlet2 responds to a get request that contains data. Line // head section of document 34 out.println( " " ); 35 out.println( 36 " Processing get requests with data " ); 37 out.println( " " ); // body section of document 40 out.println( " " ); 41 out.println( " Hello " + firstName + ", " ); 42 out.println( "Welcome to Servlets! " ); 43 out.println( " " ); // 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 HTML document in which the form’s action invokes WelcomeServlet2 through the alias welcome2 specified in web.xml. Line Processing get requests with data Type your first name and press the Submit button Get the first name from the user.

21  2002 Prentice Hall. All rights reserved. Outline Fig 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 WelcomeServlet3 responds to a post request that contains data. Lines // 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 { // 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" ); response.setContentType( "text/html" ); 19 PrintWriter out = response.getWriter(); // send XHTML page to client // start XHTML document 24 out.println( " " ); out.println( "" ); 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( " " ); // body section of document 40 out.println( " " ); 41 out.println( " Hello " + firstName + ", " ); 42 out.println( "Welcome to Servlets! " ); 43 out.println( " " ); // 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 HTML document in which the form’s action invokes WelcomeServlet3 through the alias welcome3 specified in web.xml. Lines Handling an HTTP Post Request with Data Type your first name and press the Submit button 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 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