Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)

Slides:



Advertisements
Similar presentations
Java Server Pages (JSP)
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.
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
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.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
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.
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
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 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 and JSP.
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.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
 2005 Pearson Education, Inc. All rights reserved Servlets.
1 Chapter 2 The Web Tier  Web Applications and Web ContainersWeb Applications and Web Containers  Dynamic Content CreationDynamic Content Creation 
Chapter 17 - Deploying Java Applications on the Web1 Chapter 17 Deploying Java Applications on the Web.
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.
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
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 Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface Servlet and the Servlet Life Cycle HttpServlet Class.
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
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.
A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing.
@2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING.
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
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.
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.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
How CGI and Java Servlets are Run By David Stein 14 November 2006.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Chapter 4 Request and Response. Servlets are controlled by the container.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
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.
Speaker Name Speaker Title Speaker Title Oracle Corporation Olivier Le Diouris Principal Product Manager Oracle Corporation Building Servlet and JSP Applications.
Introduction to Servlets
CS3220 Web and Internet Programming Introduction to Java Servlets
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Java Servlets.
MSIS 655 Advanced Business Applications Programming
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.
Objectives In this lesson you will learn about: Need for servlets
COP 4610L: Applications in the Enterprise Spring 2005
COP 4610L: Applications in the Enterprise Spring 2005
COP 4610L: Applications in the Enterprise Spring 2005
COP 4610L: Applications in the Enterprise Spring 2005
CS3220 Web and Internet Programming Introduction to Java Servlets
J2EE Lecture 1:Servlet and JSP
Presentation transcript:

Advanced Programming Estifanos T. (MSc in Computer Networking)

CHAPTER SEVEN Java Servlets Estifanos T. (MSc in Computer Networking)

Objectives 3 Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking) Servlet and JSP Overview Handling HTTP get Request Servlet Life Cycle Servlet and JSP Implementations JSP Components

4 Servlets, JSP, and J2EE Outlook Socketssocket reads/writes from/to data streams similar to read/write to filejava.net RMIremote method calls similar to local function calls java.rmi servletsrequest-response model javax.servlet extends server functionality javax.servlet.http JSPrequest-response on top of servlets (in fact translated into servlets) allows writing Java code directly javax.servlet.jsp into Web page - sripletsjavax.servlet.jsp.tagext Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

5 Servlets and JSPs Application Servlets are typically used for data base intensive applications with a thin client, i.e. an applications with minimal client support:  The server is responsible for data base access;  Presentation logic is written once and resides on the server;  The client accesses server through standard protocols available on most client platforms Servlets and JSPs can be used interchangeably. JSPs are an extension of servlets that allows using the request/response mechanism without getting into lower level servlet details. However, JSP are primarily used when most content sent to client is static or markup, and only a small portion is generated dynamically with Java code. As opposed to this Servlets are typically used for performing tasks on behalf of the client; they would then invoke other servlets or JSPs to provide response Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

6 Servlets and JSPs Implementation Sun initiated the development of servlets and JSP through the Java Community Process ( that allows individuals, organizations, and corporations to participate in the development of new technologies and APIs for the Java platformwww.jcp.org Reference implementation of servlets and JSP standards is under development by the Apache Software Foundation ( and is part of the Jakarta Project (jakarta.apache.org) Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

7 Servlet and JSP part of the Jakarta project is called Tomcat and is the official reference implementation of these two standards. The Jakarta project contains other subprojects Servlets and JSPs are supported directly or with third party plug-ins by most Web and application servers, e.g. Netscape iPlanet Application Server, MS' Internet Information Server (IIS), Apache HTTP Server, BEA WebLogic application server, IBM's WebSphere application server, WWW Consortium's Jigsaw Web server Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking) Servlets and JSPs Implementation

8 Servlet Overview A servlet must implement the Servlet interface Servlet interface defines methods void init( ServletConfig ) ServletConfig getServletConfig () String getServletInfo() void service( ServletRequest, ServletReply) void destroy() The servlet package has two abstract classes that implement the Servlet interface GenerigServlet from javax.servlet HttpServlet from javax.servlet.http These classes provide default implementations of all service methods and override some of these methods to provide the appropriate specification Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

9 Servlet Communication: request - reply A typical communication consists of The client sending a request to server or servlet container, The server/container receiving the request, and directing it to The appropriate servlet which does the processing, and The container sending back the result For instance a client (e.g. browser) sends an http request to server/container, the servlet does the required processing, e.g. by interacting with DB or assembling the document, interacting with other server side components (other servlets, JSPs, EJBs), then sends the document (e.g. in http, xhtml, xml, image format) to client to display in browser Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

10 Servlet Lifecycle Life cycle of servlet starts by Server (also called servlet container or servlet engine) loads servlet into memory after receiving first request; init() method is automatically invoked to initialize servlet thus providing access to the environment (container, server) on which servlet executes; Servlet responds to request; All requests are handled by service() method which is called once per request during the life cycle of the servlet. The service method can be overridden in derived classes Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

11 Example: http get request HttpServlet class differentiates between get and post request types from Web browsers and provides doGet() and doPost() methods to handle them The client invokes the servlet through a form in an XHTML document. The form action specifies the path to the servlet, and the method the request type, here a "get" URL path that invokes servlet at server side request type Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

12 Client can access servlet only if it is installed on server that can respond to servlet requests Sometimes servlet support is built directly into the Web server and no special configuration is required to handle servlet requests Alternatively, it may be necessary to integrate a servlet container with a Web server: e.g. this can be done with Tomcat (a fully functional implementation of JSP and servlet standards) for Apache Web server of the Apache Software Foundation or Microsoft's Internet Information Server (IIS) Tomcat is integrated in the Java 2 Enterprise Edition reference implementation from Sun Notes on Servlets Support Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

13 // A simple servlet to process get requests. package com.deitel.advjhtp1.servlets; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class WelcomeServlet extends HttpServlet { // process "get" requests from clients protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { //specify content (MIME)type for client browser response.setContentType( "text/html" ); //instantiate PrintWriter object to send to client PrintWriter out = response.getWriter(); //Note: for binary data use getOutputStream() //method to instantiate an ServletOutputStream //object Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking) Example: server side

14 Example: server side // send XHTML page to client // start XHTML document out.println( " " ); out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " + "XHTML 1.0 Strict//EN\" \" + "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" ); out.println( "<html xmlns = \" ); // head section of document out.println( " " ); out.println(" A Simple Servlet Example "); out.println( " " ); // body section of document out.println( " " ); out.println( " Welcome to Servlets! " ); out.println( " " ); Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

15 Example: server side // end XHTML document out.println( " " ); // commit info to client: close o-stream, //flush o-buffers, //send info to client out.close(); } Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

16 JSP Overview JSP is an extension of servlets to simplify delivery of dynamic Web content. As with servlets they execute as part of a Web server JSPs looks very much like an XHTML document, and normally include markup. JSP requests are specified in the page with corresponding syntax When the server receives the first request for JSP it translates it into a servlet that handles current and future requests (the corresponding Java statements are placed into method _ jspService); the servlet is then compiled and executed The lifecycle of the JSP is the same as the lifecycle of the servlet Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

17 JSP Components directives – messages to JSP container to specify page settings, include content from other resources, specify custom tag libraries actions – encapsulate functionality of predefined tags, that can be embedded in the JSP; often performed on info sent in client request scriplets or scripting elements – allow insertion of Java code. Generally defined as blocks of code enclosed between with expressions that are evaluated at client request defined in, e.g. to display the current Date declarations enclosed in, e.g comments enclosed in, e.g. tag libraries – part of tag extension mechanism that allows to create custom tags Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

End Of Chapter Seven