Servlet Fudamentals.

Slides:



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

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.
10-Jun-15 Servlets. 2 Servers A server is a computer that responds to requests from a client Typical requests: provide a web page, upload or download.
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.
Object-Oriented Enterprise Application Development Introduction to Servlets.
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.
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.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
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.
Java Servlets & Java Server Pages Lecture July 2013.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
1 CS122B: Projects in Databases and Web Applications Spring 2015 Notes 03: Web-App Architectures Professor Chen Li Department of Computer Science CS122B.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
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.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
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 ►
L.MARIA MICHAEL VISUWASAM UNIT-4
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.
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.
Java Servlets Java Server Pages (JSP)
Java Servlets and Java Server Pages
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.
CS122B: Projects in Databases and Web Applications Spring 2017
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Winter 2017
Servlets.
Server Side Programming
Java Servlets By: Tejashri Udavant..
CSE 403: Servlet Technology
Net-centric Computing
Introduction to Java Servlets on Jakarta Tomcat
HTTP Servlet Overview Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might.
Servlet API and Lifecycle
Chapter 26 Servlets.
CS122B: Projects in Databases and Web Applications Winter 2018
Introduction to Servlets
Servlets.
CS122B: Projects in Databases and Web Applications Spring 2018
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
Objectives In this lesson you will learn about: Need for servlets
CS122B: Projects in Databases and Web Applications Winter 2019
Introduction to Servlet
Basic servlet structure
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Servlet Fudamentals

Revisiting the Three-tier Model Web-based e-commerce applications are usually built according to the “Three-tier Model”, which includes: The First-tier: Web Client The Second-tier: Server-side Application The Third-tier: Database Management Systems (DBMS)

Three-Tier Model First-tier : Web Clients Third-tier (DBMS) : Database Cluster Client Browser First-tier : Web Clients Second-tier : Server-side Web Applications Web Server Application Server Third-tier (DBMS) : Database Cluster(s) SQL Server Other databases Oracle DB2 Internet Connectivity

Server-side programming technologies There are numerous ways to implement server-side applications. Common examples include: CGI (Common Gateway Interface) ASP (Active Server Page) Java Servlets PHP, Perl, & other server programming languages

Common Gateway Interface (CGI) Early Web page designs were “static” in the sense that a client can only request for a static HTML document from the Web server Later, CGI programming techniques were introduced to remove this constraint by providing dynamic Web pages via server-side interaction, as shown in Fig.

Static Web Page Retrieval (Fig. 4.2)  HTTP Request  HTTP Document  Retrieve Document  Request for Web Document Web Documents Web Browser Web Server

CGI-based Web Application (Fig. 4.3)  Get Data  HTTP Request  HTTP Document  Output (HTML)  HTML forms to invoke CGI scripts CGI Scripts/ Applications Web Browser Web Server Database  Return data

Disadvantages of CGI programs Each new request activates a new process to run the CGI program Creating a process requires time and resource so CGI programs is not as scaleable a solution Also, CGI programs may raise security problems.

Java Servlet A servlet is a small piece of server-side application, which can be viewed as the server-side analog of an applet. In a typical servlet application, a servlet-enabled Web server receives an HTTP request from the client. It then forwards the request to the servlet engine for performing the necessary operations as specified by the program. Finally it returns a response (e.g., HTML document) to the client via the Web server (Fig. 4.5)

Typical Web-based Servlet Interaction (Fig. 4.5) Servlet Engine HTTP Request HTTP Response Web Browser Web Server Database

What can you build with Servlets? Search Engines E-Commerce Applications Shopping Carts Product Catalogs Intranet Applications Groupware Applications: bulletin boards file sharing

Servlets vs. CGI A Servlet does not run in a separate process. A Servlet stays in memory between requests. There is only a single instance of a servlet which answers all requests concurrently. Browser 1 Web Server Browser 2 Browser N Servlet

How Servlets Work is servlet No Receive loaded? Request Yes is servlet current? No Load Servlet Yes Send Response Process Request

Servlet Architecture The client makes a request via HTTP Web Server Servlet Container Client (web browser) HTTP request Servlet HTTP response The client makes a request via HTTP The web server receives the requests and forwards it to the servlet If the servlet has not yet been loaded, the web server loads it into the JVM and executes it The servlet receives the HTTP request and performs some type of process The servlet returns a response to the web server The web server forwards the response to the client

Servlets follow a three-phase life cycle: Servlet Life Cycle Servlets follow a three-phase life cycle: 1) initialization 2) service 3) destruction

Life Cycle: Initialization A servlet container: loads a servlet class during startup, or when the servlet is needed for a request After the Servlet class is loaded, the container will instantiate it.

Life Cycle: Initialization Initialization is performed by container before any request can be received. Persistent data configuration, heavy resource setup (such as JDBC connection) and any one-time activities should be performed in this state. The init() method will be called in this stage.

Life Cycle: Service 1 The service method is defined for handling client request. The Container of a servlet will call this method every time a request for that specific servlet is received.

Life Cycle: Service The Container generally handles concurrent requests with multithreads. All interactions with response and requests will occur within this phase until the servlet is destroyed.

Life Cycle: Service Method The service() method is invoked to every request and is responsible for generating the response to that request. The service() method takes two parameters: javax.servlet.ServletRequest javax.servlet.ServletResponse public void service ( ServletRequest request, ServletResponse response ) throws IOException { . . . }

Life Cycle: Destruction When the servlet container determines that the servlet should be removed, it calls the destroy method of the servlet. The servlet container waits until all threads running in the service method have been completed or time out before calling the destroy method.

Types of Servlet Generic Servlet Http Servlet javax.servlet (package) extends javax.servlet.Servlet service method Http Servlet javax.servlet.http (package) extends javax.servlet.HttpServlet doget(), doPost()….

Types of servlets (cont..) Generic servlet service(Request, Response) throws ServletException, IOException HttpServlet doGet(HttpServletRequest req, HttpServletResponse res) doPost(HttpServletRequest req, HttpServletResponse res)

Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet javax.servlet.http.HttpServlet

Generic Servlet & HTTP Servlet Client request Server service ( ) response HTTPServlet Browser request doGet ( ) HTTP Server service ( ) response doPost ( )

Interface javax.servlet.Servlet The Servlet interface defines methods to initialize a servlet to receive and respond to client requests to destroy a servlet and its resources to get any startup information to return basic information about itself, such as its author, version and copyright. Developers need to directly implement this interface only if their servlets cannot (or choose not to) inherit from GenericServlet or HttpServlet. Life Cycle Methods

GenericServlet - Methods void init(ServletConfig config) Initializes the servlet. void service(ServletRequest req, ServletResponse res) Carries out a single request from the client. void destroy() Cleans up whatever resources are being held (e.g., memory, file handles, threads) and makes sure that any persistent state is synchronized with the servlet's current in-memory state. ServletConfig getServletConfig() Returns a servlet config object, which contains any initialization parameters and startup configuration for this servlet. String getServletInfo() Returns a string containing information about the servlet, such as its author, version, and copyright.

HTTPServlet A general servlet knows nothing about the HyperText Transfer Protocol (HTTP), which is the major protocol used for Internet. A special kind of servlet, HTTPServlet, is needed to handle requests from HTTP clients such as web browsers. HTTPServlet is included in the package javax.servlet.http as a subclass of GenericServlet.

Hello World Example import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println ("<!DOCTYPE HTML PUBLIC \”-//W3C//DTD HTML 4.0 Transitional//EN\”>"); out.println("<HTML><HEAD><TITLE>Hello World</TITLE></HEAD>"); out.println(“<BODY><BIG>Hello World </BIG></BODY></HTML>"); out.close(); }

Hello World Example import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println ("<!DOCTYPE HTML PUBLIC \”-//W3C//DTD HTML 4.0 Transitional//EN\”>"); out.println("<HTML><HEAD><TITLE>Hello World</TITLE></HEAD>"); out.println(“<BODY><BIG>Hello World </BIG></BODY></HTML>"); out.close(); }

DEMO