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

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

Servlets & JSPs - Sharad Ballepu.
Internet i jego zastosowania 1 J2EE Servlets. Internet i jego zastosowania 2 Agenda Overview Servlet Interface Servlet Context Request Response Sample.
Servlets. Servlets are modules that extend Java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
Communicating in J2EE.
19 Copyright © 2005, Oracle. All rights reserved. Distributing Modular Applications: Developing Web Services.
21 Copyright © 2005, Oracle. All rights reserved. Oracle Application Server 10g Transaction Support.
7 Copyright © 2005, Oracle. All rights reserved. Maintaining State in J2EE Applications.
18 Copyright © 2005, Oracle. All rights reserved. Distributing Modular Applications: Introduction to Web Services.
Preface IIntroduction Objectives I-2 Course Overview I-3 1Introducing the Java and Oracle Platforms Objectives 1-2 What Is Java? 1-3 Key Benefits of Java.
5 Copyright © 2005, Oracle. All rights reserved. Accessing the Database with Servlets.
16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.
11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
6 Copyright © 2005, Oracle. All rights reserved. Using Advanced Techniques in Servlets.
15 Copyright © 2005, Oracle. All rights reserved. Container-Managed Relationships (CMRs)
1 Copyright © 2005, Oracle. All rights reserved. Introducing the Java and Oracle Platforms.
9 Copyright © 2005, Oracle. All rights reserved. Modularizing JavaServer Pages Development with Tags.
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
J2EE Overview.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
6 Copyright © 2005, Oracle. All rights reserved. Building Applications with Oracle JDeveloper 10g.
17 Copyright © 2005, Oracle. All rights reserved. Deploying Applications by Using Java Web Start.
CGI programming. Common Gateway Interface interface between web server and other programs (cgi scripts) information passed as environment variables passed.
M-V-C for web applications. Model for Web Applications model consists of data and system state database tables –persistent data session information –current.
1 Web Search Interfaces. 2 Web Search Interface Web search engines of course need a web-based interface. Search page must accept a query string and submit.
Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
PSSA Preparation.
JLab Lattice Portal – Data Grid Web Service Ying Chen, Chip Watson Thomas Jefferson National Accelerator Facility.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Java Server Pages (JSP)
Java Servlets Java Server Pages (JSP)
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 24: Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface.
Objectives Ch. D - 1 At the end of this chapter students will: Know the general architecture and purpose of servlets Understand how to create a basic servlet.
MC365 Application Servers: Servlets. Today We Will Cover: What a servlet is The HTTPServlet and some of its more important methods How to configure the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.
Servlets Life Cycle. The Servlet Life Cycle A servlet life cycle can be defined as the entire process from its creation till the destruction. The following.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
Java Servlets & Java Server Pages Lecture July 2013.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
L.MARIA MICHAEL VISUWASAM UNIT-4
Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets.
1 Java Servlets l Servlets : programs that run within the context of a server, analogous to applets that run within the context of a browser. l Used to.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
JS (Java Servlets). Internet evolution [1] The internet Internet started of as a static content dispersal and delivery mechanism, where files residing.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
Introduction to Servlets
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Chapter 26 Servlets.
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
Presentation transcript:

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

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

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

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

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.

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 ()

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

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

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"); }

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

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

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()

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()

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.

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 (" "); }

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.

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

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

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

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

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… }

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

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

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

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 …

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

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

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

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

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

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

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

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

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

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

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