1 / 57

Chapter 36 - Servlets: Bonus for Java™ Developers

Chapter 36 - Servlets: Bonus for Java™ Developers.

khanh
Download Presentation

Chapter 36 - Servlets: Bonus for Java™ Developers

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 36 - Servlets: Bonus for Java™ Developers Outline36.1 Introduction36.2 Servlet Overview and Architecture 36.2.1 Interface Servlet and the Servlet Life Cycle 36.2.2 HttpServlet Class 36.2.3 HttpServletRequest Interface 36.2.4 HttpServletResponse Interface36.3 Handling HTTP get Requests 36.3.1 Setting Up the Apache Tomcat Server 36.3.2 Deploying a Web Application36.4 Handling HTTP get Requests Containing Data36.5 Handling HTTP post Requests36.6 Redirecting Requests to Other Resources36.7 Multi-Tier Applications: Using JDBC from a Servlet36.8 Web Resources

  2. Objectives • In this lesson, you will learn: • To execute servlets with the Apache Tomcat server. • To be able to respond to HTTP requests from an HttpServlet. • To be able to redirect requests to static and dynamic Web resources.

  3. 36.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

  4. 36.1 Introduction (Cont.) • Servlets • Thin clients • Request/response mechanism • redirection • Tomcat • Jakarta project • Official reference implementation of the JSP and servlet standards

  5. 36.2 Servlet Overview and Architecture • Servlet container (servlet engine) • Server that executes a servlet • Web servers and application servers • Sun ONE 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

  6. 36.2.1 Interface Servlet and the Servlet Life Cycle • Interface Servlet • All servlets must implement this interface • All methods of interface Servlet are invoked by servlet container • 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

  7. 36.2.1 Interface Servlet and the Servlet Life Cycle (Cont.)

  8. 36.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

  9. 36.2.2 HttpServlet Class (Cont.)

  10. 36.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

  11. 36.2.3 HttpServletRequest Interface (Cont.)

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

  13. 36.2.4 HttpServletResponse Interface (Cont.)

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

  15. 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. Import the javax.servlet and javax.servlet.http packages. Create the XHTML document by writing strings with the out object’s println method. Uses the response object’s setContentType method to specify the content type of the data to be sent as the response to the client. WelcomeServlet.java(1 of 2)

  16. Closes the output stream, flushes the output buffer and sends the information to the client. WelcomeServlet.java(2 of 2)

  17. WelcomeServlet.html(1 of 1)

  18. Program output

  19. 36.3.1 Setting Up the Apache Tomcat Server • Download Tomcat (version 4.1.27) • jakarta.apache.org/site/binindex.cgi • Define environment variables • JAVA_HOME • CATALINA_HOME • Start the Tomcat server • startup • Launch the Tomcat server • http://localhost:8080/

  20. 36.3.1 Setting Up the Apache Tomcat Server (Cont.). Fig. 36.7 Tomcat documentation home page. (Courtesy of The Apache Software Foundation.)

  21. 36.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

  22. 36.3.2 Deploying a Web Application (Cont.)

  23. 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. web.xml(1 of 2)

  24. Element servlet-class specifies compiled servlet’s fully qualified class name. Element servlet-mapping specifies servlet-name and url-pattern elements. web.xml(2 of 2)

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

  26. 36.3.2 Deploying a Web Application (Cont.)

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

  28. The request object’s getParameter method receives the parameter name and returns the corresponding String value. WelcomeServlet2(1 of 2)

  29. Uses the result of line 15 as part of the response to the client. WelcomeServlet2(2 of 2)

  30. Get the first name from the user. WelcomeServlet2.html(1 of 1)

  31. Program output

  32. 36.4 Handling HTTP get Requests Containing Data (Cont.)

  33. 36.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

  34. Declare a doPost method to responds to post requests. WelcomeServlet3.java(1 of 2)

  35. WelcomeServlet3.java(1 of 2)

  36. 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. WelcomeServlet3.html(1 of 1)

  37. Program output

  38. 36.5 Handling HTTP post Requests (Cont.)

  39. 36.6 Redirecting Requests to Other Resources • Servlet RedirectServlet • Redirects the request to a different resource

  40. Redirects the request to the servlet WelcomeServlet. Obtains the page parameter from the request. Determine if the value is either “deitel” or “welcome1” Redirects the request to www.deitel.com. RedirectServlet.java(1 of 3)

  41. Output a Web page indicating that an invalid request was made if method sendRedirect is not called. RedirectServlet.java(2 of 3)

  42. RedirectServlet.java(3 of 3)

  43. Provide hyperlinks that allow the user to invoke the servlet RedirectServlet. RedirectServlet.html(1 of 1)

  44. Program output

  45. 36.6 Redirecting Requests to other Resources (Cont.)

  46. 36.7 Multi-Tier Applications: Using JDBC from a Servlet • Three-tier distributed applications • User interface • Business logic • Database access • Web servers often represent the middle tier • Three-tier distributed application example • SurveyServlet • Survey.html • MS Access database

  47. Attempt to open a connection to the animalsurvey database. Loads the database driver. Servlets are initialized by overriding method init. SurveyServlet.java(1 of 6)

  48. Create Statement to query database. SurveyServlet.java(2 of 6)

  49. Obtain the survey response Create query to update total for current survey response Execute queryto update total for current survey response SurveyServlet.java(3 of 6)

  50. Create query to get total of all survey responses Execute queryto get total of all survey responses Create query to get survey results Execute query to get survey results SurveyServlet.java(4 of 6)

More Related