Object-Oriented Enterprise Application Development Introduction to Servlets.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
Advertisements

 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Java Servlet & JSP © copyright 2005 SNU OOPSLA Lab.
J.Sant Servlets Joseph Sant Sheridan Institute of Technology.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
Introduction to Servlet & JSP
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Java Server and Servlet CS616 Team 9 Kim Doyle, Susan Kroha, Arunima Palchowdhury, Wei Xu.
Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET.
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.
Session-02.
Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Server-side Technologies
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
1 Servlet How can a HTML page, displayed using a browser, cause a program on a server to be executed?
Java Servelets. What Is a Servlet? A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed.
Servlets Pranav Maydeo. What is a Servlet ? Servlets are modules of Java code that run in a server application to answer client requests. Servlets are.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
Copyright, 1996 © Dale Carnegie & Associates, Inc. Presented by Hsiuling Hsieh Christine Liu.
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
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.
1 CS122B: Projects in Databases and Web Applications Spring 2015 Notes 03: Web-App Architectures Professor Chen Li Department of Computer Science CS122B.
Servlets Server-Side Software. Objective What is a servlet? Where do servlets fit in? What can servlets do? Why are servlets better than CGI? Servlet.
Chapter 2 Web app architecture. High-level web app architecture  When a client request coming in and needs servlet to serve dynamic web content, what.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
Java Servlets and Java Server Pages Norman White Stern School of Business.
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.
Servlets.
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.
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.
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Java Servlets and Java Server Pages
Introduction To HTML Dr. Magdi AMER. HTML elements.
How CGI and Java Servlets are Run By David Stein 14 November 2006.
Chapter 4 Request and Response. Servlets are controlled by the container.
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.
1 Lecture 8 George Koutsogiannakis/Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
1 Web Programming with Servlets & JSPs WEB APPLICATIONS – AN OVERVIEW.
Speaker Name Speaker Title Speaker Title Oracle Corporation Olivier Le Diouris Principal Product Manager Oracle Corporation Building Servlet and JSP Applications.
CS122B: Projects in Databases and Web Applications Spring 2017
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Winter 2017
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Introduction to Java Servlets on Jakarta Tomcat
Servlet API and Lifecycle
Servlet.
Chapter 26 Servlets.
CS122B: Projects in Databases and Web Applications Winter 2018
CS122B: Projects in Databases and Web Applications Spring 2018
Objectives In this lesson you will learn about: Need for servlets
J2EE Lecture 1:Servlet and JSP
CS122B: Projects in Databases and Web Applications Winter 2019
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Object-Oriented Enterprise Application Development Introduction to Servlets

Topics During this class we will examine: Motivation Require packages Classes Lifecycle

Web Applications

Development Approaches In the past few years, many alternative technologies have been presented for web application development: CGI Proprietary APIs Server-Side JavaScript Microsoft ASPs Java Servlets

CGI (1 of 2) In a CGI application, each client request executes a program on the server. These are typically written in C, C++ or Perl. Could be embedded within a web server and thus didn't require any new software.

CGI (2 of 2) While this satisfies the requirement for dynamic content, there are potential problems: Starting a new process is expensive Poorly written code is a significant security hazard. Difficult to communicate between the CGI program and the client.

Proprietary APIs Many vendors offered proprietary APIs: Netscape:NSAPI Microsoft:ISAPI O'Reilly:WSAPI Since they're proprietary, you're binding your architecture to a particular vendor.

Server-Side JavaScript This is JavaScript that's placed into precompiled HTML pages and then executed on the server. Improved performance. Improved security. Only a few products actually support this technique.

Microsoft ASP This is similar to server-side JavaScript, but the code isn't precompiled. This technique is tied to the Microsoft IIS application server or other products where it's an add-on module.

Enterprise Java As opposed to investing in proprietary techniques, many vendors have started backing the use of enterprise Java. Java has a well-defined API. Java has been widely accepted by the industry for its portability. This will be the focus of this course.

Servlet Background

Servlets Defined Java servlets are precompiled Java programs that are executed on a server. Servlets are installed in a web container that is responsible for locating, loading, and executing the servlet when an appropriate request is issued.

Essential Benefits Because they're written in Java and executed on the server, servlets provide: Efficiency Persistence Portability Robustness Extensibility Security

Efficiency (1 of 2) Servlets are loaded and initialized only once: when the servlet engine receives the first request for that servlet. Servlet engines automatically multi-thread each servlet. There is only a single instance of any given servlet in memory. Each request for that servlet is a new thread that shares the servlet instance.

Efficiency (2 of 2) Many web servers load balance servlet requests across multiple physical servers to improve performance. This results in better performance and fault tolerance.

Persistence (1 of 2) Servlets can hold data that spans across the various requests received by various clients. This can improve performance by treating each servlet as a singleton. This persistent data is lost if the servlet engine unloads the servlet class from memory.

Persistence (2 of 2) This persistence mechanism doesn't provide session persistence for each client. This means that applications built on the familiar "shopping cart" metaphor must use a different approach to persist the data for a specific user of that application. We'll address this technique during the next lecture.

Portability Because the servlets are written in pure Java, they can be moved into any environment that supports servlets. Always make sure you confirm the version of the JDK that you used to write your servlets as well as the version of the servlet specification.

Robustness Because servlets are written in Java, they have access to the entire JDK. This includes all inherent Java capabilities such as inheritance, polymorphism, and exception handling.

Extensibility Like all other classes, we can design servlets that are base classes that provide basic functionality. We can then inherit from these base servlets to provide additional functionality. This allows the use of relevant design patterns such as template method or command.

Security Because servlets are executed on the server, the client never gains access to the code. Servlets can also take advantage of the security manager and cryptography classes. There is also code security; a servlet that terminates abnormally usually won't crash the servlet engine.

Servlet Lifecycle

Servlet Lifecycle (1 of 4) All servlets follow the same lifecycle. The init() method is called when the servlet class is first loaded by the servlet engine. The service() method is called each time the servlet is requested by a client. The destroy() method is called just before the servlet engine unloads the servlet class from memory.

Servlet Lifecycle (2 of 4) The init() method is where a servlet's life begins. This method is only called when the servlet class is loaded by the servlet engine. If you override this method be sure to call the parent class' init() method. This makes the init() method useful for doing one-time servlet configuration.

Servlet Lifecycle (3 of 4) The service() method is where a servlet spends most of its life. This method is invoked one (1) time for each client request made to the servlet. This method typically acts as a dispatcher by interrogating the client's request and acting on the data it finds there.

Servlet Lifecycle (4 of 4) The destroy() method is where a servlet's life ends. This method is only called when the servlet class is unloaded by the servlet engine. If you override this method be sure to call the parent class' destroy() method. This makes the destroy() method useful for doing one-time servlet cleanup.

Servlets & Java

Required Packages (1 of 2) Servlets are included in J2EE or in the J2SDK as an additional download. The are two (2) packages required for servlets: javax.servlet.* javax.servlet.http.*

Required Packages (2 of 2) The javax.servlet package provides the basic interfaces that must be implemented and extended by all servlets. The javax.servlet.http package provides the key classes used to construct servlet-based applications using the HTTP protocol.

Servlets At the heart of each servlet is the Servlet interface containing the following methods: destroy getServletConfig getServletInfo init service

Generic Servlets For non-HTTP based servlets, we inherit from the GenericServlet class. GenericServlets provide default implementations for the init() and destroy() methods. However, servlets inherited from this class must implement the service() method.

Generic Requests When a request is issued to the servlet, we must be able to process that request. This may require the parsing of arguments or other complex actions specific to our implementation. In the generic case this means that we need to construct a class that implements the ServletRequest interface.

Generic Responses Once the servlet has completed its work, it needs to send any results back to the client who made the original request. In the generic case this means that we need to construct a class that implements the ServletResponse interface.

Generic Dispatchers Sometimes we may want to delegate a request made to one servlet to an entirely different servlet. This is accomplished using a class that implements the RequestDispatcher interface.

HTTP Servlets For HTTP based servlets, we inherit from the HttpServlet class. This class provides a default service() method that we won't usually override. This method knows how to parse requests made using the HTTP protocol.

Sample Code – HelloWorld (1 of 2) 1.import java.io.*; 2.import javax.servlet.*; 3.import javax.servlet.http.*; 4.public class HelloWorld extends HttpServlet { 5. public void doGet(HttpServletRequest rqst, HttpServletResponse resp) throws IOException, ServletException { 6. resp.setContentType("text/html"); 7. PrintWriter out = new PrintWriter( resp.getOutputStream() );

Sample Code – HelloWorld (2 of 2) 8. out.println(" "); 9. out.println(" "); 10. out.println(" "); 11. out.println("HelloWorld"); 12. out.println(" "); 13. out.println(" "); 14. out.println(" "); 15. out.println(" Hello, World! "); 16. out.println(" "); 17. out.println(" "); 18. out.close(); 19. } 20.}

HTTP Servlet Anatomy (1 of 2) The HelloWorld servlet illustrates some of the key servlet elements. The servlet extends the HttpServlet class. The doGet() method accepts two (2) arguments and throws two (2) exceptions.

HTTP Servlet Anatomy (2 of 2) But how is doGet() invoked? The answer lies in the default implementation of the service() method within the HttpServlet class. When a GET request is received, the service() method automatically invokes the doGet() method.

Initialization Parameters

Justification Sometimes we want to initialize a servlet when it's first loaded by the servlet engine. For instance, we might want to load some configuration settings before allowing processing to continue. This can be useful for testing when you don't want to configure a web page to initiate the test.

Technique We can embed such values in the application's web.xml file. We access these values within the servlet's init() method when it is first loaded. To access these initialization parameters we use the getInitParameter() method on the ServletConfig reference passed to the init() method.

Sample Code – Message (1 of 2) 1.import java.io.*; 2.import javax.servlet.*; 3.import javax.servlet.http.*; 4.public class Message extends HttpServlet { 5. private String msg = null; 6. public void init( ServletConfig cfg ) throws ServletException { 7. super.init( cfg ); 8. msg = cfg.getInitParameter("msg"); 9. if ( msg == null) { 10. msg = "no.message.specified"; 11. } 12. }

Sample Code – Message (2 of 2) 13. resp.setContentType("text/html"); 14. PrintWriter out = new PrintWriter( resp.getOutputStream() ); 15. out.println(" "); 16. out.println("Message"); 17. out.println(" "); 18. out.println(" "); 19. out.println(" " + msg + " "); 20. out.println(" "); 21. out.close(); 22. } 23.}

Servlet Pitfalls

Local Data Members (1 of 3) In the Message servlet, I declared a local data member called msg. With regular classes, this data member would be specific to each object. For servlets, this data member is shared across all requests made to the Message servlet.

Local Data Members (2 of 3) This means that we can't use local data members to store user- or session-specific data. Each new access to that data may result in the value being changed. This change will affect all users of that servlet. However, such data members are useful if the value in question is meant to be a constant.

Local Data Members (3 of 3) To pass the various data members between methods within the servlet, we are forced to adopt the approach of using an aggregate class. This could be something as simple as using a Java Collection class. We could also devise our own custom class.

Review During this class we have discussed: Motivation Require packages Classes Lifecycle

Resources Developing Java Servlets James Goodwill, Sam's Publishing, ISBN: Core Servlets and JavaServer Pages Marty Hall, Prentice-Hall, Inc., ISBN: Java 2 Platform, Enterprise Edition B. Shannon, et al., Addison-Wesley, ISBN:

Coming Attractions Next week we'll look at servlets in greater depth including how to invoke them from an HTML page and how to implement the "shopping cart" metaphor. Please read Chapters 3 & 9 in your text.