Presentation is loading. Please wait.

Presentation is loading. Please wait.

4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.

Similar presentations


Presentation on theme: "4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets."— Presentation transcript:

1 4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets

2 4-2 Copyright © 2005, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Define the role of servlets in a J2EE application Describe the servlet life cycle Describe the request and response architecture Implement HTTP servlet methods List J2EE servlet mapping techniques Handle errors in a servlet Create and run a servlet in JDeveloper Deploy a J2EE application to Oracle Application Server 10g

3 4-3 Copyright © 2005, Oracle. All rights reserved. Dynamic HTML Client Web browserServlet Connects to Generates Overview Requests Responds to

4 4-4 Copyright © 2005, Oracle. All rights reserved. Client Server Servlet engine Java application, Servlet, JSP, or HTML About Java Servlets A servlet is a Java class that implements the Servlet interface. A servlet runs in the context of a special process called a servlet engine. Servlets can be invoked simultaneously by multiple clients. Request Response

5 4-5 Copyright © 2005, Oracle. All rights reserved. Server Clients Request 1 Request 2 Request 3 Principal Features of Servlets Concurrent requests are possible and common. Servlet methods are run in threads. Servlet instances are shared by multiple client requests.

6 4-6 Copyright © 2005, Oracle. All rights reserved. Load Life Cycle of Servlets All actions are carried out inside the server. After initial setup, the response time is less. Initialize init() Destroy destroy () Execute service ()

7 4-7 Copyright © 2005, Oracle. All rights reserved. Request Response HTTP Servlets HTTP servlets extend the HttpServlet class, which implements the Servlet interface. A client makes an HTTP request, which includes a method type that: –Can be either a GET or POST method type –Determines what type of action the servlet will perform The servlet processes the request and sends back a status code and a response. HTTP protocol Client Servlet

8 4-8 Copyright © 2005, Oracle. All rights reserved. The servlet overrides the doGet() or the doPost() method of the HttpServlet class. The servlet engine calls the service() method, which in turn calls one of the appropriate doXxx() methods. These methods take two arguments as input: – HttpServletRequest – HttpServletResponse Browser HttpServlet subclass service() doGet() Inside an HTTP Servlet Request Response

9 4-9 Copyright © 2005, Oracle. All rights reserved. Servlet: Example import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class SimplestServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("Hello World"); }

10 4-10 Copyright © 2005, Oracle. All rights reserved. The doGet() Method The most common HTTP request method type made to a Web server is GET. The service() method in your servlet invokes the doGet() method. The service() method is invoked on your behalf by the Web server and the servlet engine. The doGet() method receives two parameters as input: – HttpServletRequest – HttpServletResponse Pass parameters by appending them to the URL

11 4-11 Copyright © 2005, Oracle. All rights reserved. The doPost() Method The doPost() method can be invoked on a servlet from an HTML form via the following: The service() method in your servlet invokes the doPost() method. The service() method is invoked by the Web server and the servlet engine. The doPost() method receives two parameters as input: – HttpServletRequest – HttpServletResponse Pass parameters using the form field names

12 4-12 Copyright © 2005, Oracle. All rights reserved. The HttpServletRequest Object The HttpServletRequest object encapsulates the following information about the client: –Servlet parameter names and values –The remote host name that made the request –The server name that received the request –Input stream data You invoke one of several methods to access the information: – getParameter(String name) – getRemoteHost() – getServerName()

13 4-13 Copyright © 2005, Oracle. All rights reserved. The HttpServletResponse Object The HttpServletResponse object encapsulates information that the servlet has generated: –The content length of the reply –The MIME type of the reply –The output stream You invoke one of several methods to produce the information: – setContentLength(int length) – setContentType(String type) – getWriter()

14 4-14 Copyright © 2005, Oracle. All rights reserved. Methods for Invoking Servlets Invoke servlets from a client by: –Typing the servlet URL in a browser –Embedding the servlet URL in an HTML or a JavaServer Page (JSP) page, or another servlet (an href link) –Submitting a form to the servlet (via the action tag) –Using URL classes in client Java applications Invoke servlets inside the J2EE container by defining a chain of servlets or JSPs.

15 4-15 Copyright © 2005, Oracle. All rights reserved. Your First Servlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ response.setContentType ("text/html"); PrintWriter out = response.getWriter(); out.println (" "); out.println ("Hello World!"); out.println (" "); }

16 4-16 Copyright © 2005, Oracle. All rights reserved. Please enter your name. Thank you. Handling Input: The Form You can use an HTML form and the doPost() method to modify the HelloWorld servlet.

17 4-17 Copyright © 2005, Oracle. All rights reserved. public class NewHelloWorld extends HttpServlet { public void doPost( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{ res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println (" "); String name = req.getParameter("firstName"); if ((name != null) && (name.length() > 0)) out.println ("Hello: " + name + " How are you?"); else out.println ("Hello Anonymous!"); out.println (" "); }} Handling Input: The Servlet

18 4-18 Copyright © 2005, Oracle. All rights reserved. Initialization and Destruction Servlets also define the init() and destroy() methods in addition to the service() method. init(): –Can be used to retrieve initialization parameters –Takes a ServletConfig object as a parameter –Is invoked when the servlet instance is created –Is useful for obtaining database connections from a connection pool destroy(): –Takes no arguments –Is invoked when the servlet is about to be unloaded –Is useful for releasing resources

19 4-19 Copyright © 2005, Oracle. All rights reserved. Error Handling ServletException : –Is generated to indicate a generic servlet problem –Is subclassed by UnavailableException to indicate that a servlet is unavailable, either temporarily or permanently –Is handled by the servlet engine in implementation- dependent ways IOException : Is generated if there is an input or output error while processing the request

20 4-20 Copyright © 2005, Oracle. All rights reserved. Debugging a Servlet Servlets can be debugged in the following ways: Setting breakpoints and using the debugger in JDeveloper Viewing the source of the generated HTML

21 4-21 Copyright © 2005, Oracle. All rights reserved. SingleThreadModel You can implement the SingleThreadModel interface to prevent multithreaded access of data. Each concurrent request then has its own dedicated servlet instance, which is randomly assigned. public class HelloWorld extends HttpServlet implements SingleThreadModel{ public void doGet… }

22 4-22 Copyright © 2005, Oracle. All rights reserved.

23 4-23 Copyright © 2005, Oracle. All rights reserved. JDeveloper Environment The Servlet Wizard in JDeveloper makes it easy for you to write servlets. The wizard: Provides the doGet() and doPost() method skeletons Provides an environment for running the servlet within the integrated development environment (IDE) Dynamically creates the web.xml file for running the servlet from the IDE Allows the creation of a deployment file that aids in deploying to an OC4J server

24 4-24 Copyright © 2005, Oracle. All rights reserved. Servlet Mapping Mapping a servlet refers to how a client can access a servlet. You can map a servlet: –To any URL that begins with a certain directory name –By using the direct URL: /servlet/. –By using the mapped URL: /servlet/ is the mapping for the Web module

25 4-25 Copyright © 2005, Oracle. All rights reserved. Servlet Mapping in JDeveloper JDeveloper provides the standard J2EE model for mapping servlets by using the web.xml file: MyFirstServlet package1.HelloWorld MyFirstServlet /helloworld …

26 4-26 Copyright © 2005, Oracle. All rights reserved. Invoking a Servlet

27 4-27 Copyright © 2005, Oracle. All rights reserved. Specifying J2EE Web Module Settings

28 4-28 Copyright © 2005, Oracle. All rights reserved. Creating a Connection to Oracle Application Server 10g

29 4-29 Copyright © 2005, Oracle. All rights reserved. Deploying to OC4J

30 4-30 Copyright © 2005, Oracle. All rights reserved. Summary In this lesson, you should have learned how to: Describe the servlet life cycle Develop and run a servlet in JDeveloper Map a servlet in a J2EE server Collect information from a client Respond to the client Handle errors in a servlet Deploy a servlet to Oracle Application Server 10g

31 4-31 Copyright © 2005, Oracle. All rights reserved. Practices 4-1, 4-2, and 4-3: Overview These practices cover the following topics: Creating a servlet that invokes the doPost() method and running it from an HTML form Creating a servlet that invokes the doGet() method to create an HTML form Deploying a servlet to Oracle Application Server 10g

32 4-32 Copyright © 2005, Oracle. All rights reserved.

33 4-33 Copyright © 2005, Oracle. All rights reserved.

34 4-34 Copyright © 2005, Oracle. All rights reserved.

35 4-35 Copyright © 2005, Oracle. All rights reserved.

36 4-36 Copyright © 2005, Oracle. All rights reserved.


Download ppt "4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets."

Similar presentations


Ads by Google