1 Chapter 2 The Web Tier  Web Applications and Web ContainersWeb Applications and Web Containers  Dynamic Content CreationDynamic Content Creation 

Slides:



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

CGI programming. Common Gateway Interface interface between web server and other programs (cgi scripts) information passed as environment variables passed.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
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.
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.
An introduction to Java Servlet Programming
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
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. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
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.
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.
Introduction to JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
Objectives Java Servlet Web Components
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.
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
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.
Webcommerce Computer Networks Webcommerce by Linnea Reppa Douglas Martindale Lev Shalevich.
Java Servlets & Java Server Pages Lecture July 2013.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
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.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
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.
L.MARIA MICHAEL VISUWASAM UNIT-4
An Introduction to JavaServer™ Pages Prepared by Nicole Swan.
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
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.
An Introduction to JavaServer™ Pages Prepared by Nicole Swan.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
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.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
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,
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
How CGI and Java Servlets are Run By David Stein 14 November 2006.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
April 20023CSG11 Electronic Commerce Java (2) John Wordsworth Department of Computer Science The University of Reading Room 129,
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.
Speaker Name Speaker Title Speaker Title Oracle Corporation Olivier Le Diouris Principal Product Manager Oracle Corporation Building Servlet and JSP Applications.
Introduction to Servlets
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Chapter 26 Servlets.
Java Server Pages.
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
J2EE Lecture 1:Servlet and JSP
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

1 Chapter 2 The Web Tier  Web Applications and Web ContainersWeb Applications and Web Containers  Dynamic Content CreationDynamic Content Creation  Application DesignsApplication Designs  Servlets TechnologyServlets Technology  JSP TechnologyJSP Technology

2 Web Applications and Web Containers  Web application A collection of HTML/XML documents, Web components and other resources Provides dynamic and interactive content to browser-based clients.  Web container A runtime environment for a Web application Provides Web components with a naming context and life cycle management.

3 Dynamic Content Creation  Common Gateway Interface (CGI) scripts  Java servlets technology  Java Server Pages technology

4 Servlets  Portable platform- and Web server-independent  Extensible  Perform better than CGI scripts

5 JavaServer Pages Technology  Providing a declarative, presentation-centric method  Content and display logic are separated  Reusing code through a component-based architecture

6 Application Designs

7 Applications with Basic JSP Pages and Servlets

8 JSP pages with JavaBeans components

9 EJB-Centric Applications  Model - Represents the data on which an application is based  View - Presents the data represented by the model in a way that's targeted at a specific type of client  Controller -A Central point of control. Maintains the data in the model and ensures data and view consistency.

10 Controller Conversion of HTTP Request to Model Change Event

11 Servlet Technology What Is a Servlet? Servlet API and Servlet life cycle HTTP Support Security Issues

12 What Is a Servlet? A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed via a request- response programming model.

13  A servlet is a Java component that can be plugged into a Java-enabled web server to provide custom services: New features Runtime changes to content Runtime changes to presentation New standard protocols (such as FTP) New custom protocols  Servlets are designed to work within a request/response processing model. What Is a Servlet?

14 Servlet API Defining the interface between servlets and servers: Package javax.servlet Package javax.servlet.http

15 Servlet API Functions  Servlet life cycle management  Access to servlet context  Utility classes  HTTP-specific support classes

16 Servlet Life Cycle  three main methods: init() service() destroy()  two ancillary methods: getServletConfig() getServletInfo()

17 Servlet life cycle  If an instance of the servlet does not exist, the Web container Loads the servlet class. Creates an instance of the servlet class. Initializes the servlet instance by calling the init method. Invokes the service method, passing a request and response object.  If the container needs to remove the servlet, it finalizes the servlet by calling the servlet's destroy method.

18 Writing Service Methods The general pattern for a service method is to extract information from the request, access external resources, and then populate the response based on that information. Getting Information from Requests Constructing Responses

19 Getting Information from Requests All requests implement the ServletRequest interface. This interface defines methods for accessing parameters, etal. An input stream can be retrieved from the request for manually parsing the data: –getInputStream( ) –getReader( ) HTTP servlets are passed an HTTP request object which contains the request URL, HTTP headers, query string, and so on.

20 Constructing Responses All responses implement the ServletResponse interface: Retrieve an output stream to use to send data to the client. –getWriter(), return PrintWriter –getOutputStream( ), return ServletOutputStream Indicate the content type (for example, text/html), being returned by the response. Indicate whether to buffer output Set localization information.

21 Sample Servlet-1 import java.io.*; import javax.servlet.*; public class SampleServlet implements Servlet { private ServletConfig config; public void init (ServletConfig config) throws ServletException { this.config = config; } public void destroy() {} // do nothing public ServletConfig getServletConfig() { return config; } public String getServletInfo() { return "A Simple Servlet"; }

22 public void service (ServletRequest req, ServletResponse res ) throws ServletException, IOException { res.setContentType( "text/html" ); PrintWriter out = res.getWriter(); out.println( " " ); out.println( " ); out.println( " A Sample Servlet " ); out.println( " " ); out.println( " A Sample Servlet " ); out.println( " " ); out.close(); } Sample Servlet-2

23 Servlet Context  Servlet Initialization Information  Server Context Information  Servlet Context During a Service Request

24 Utility Classes  Interface javax.servlet.SingleThreadModel for writing simple servlets  Two exception classes javax.servlet.ServletException javax.servlet.UnavailableException

25 HTTP Protocal HTTP defines a set of text-based request messages called HTTP methods. The HTTP methods include: –GET : Retrieves the resource identified by the request URL –HEAD : Returns the headers identified by the request URL –POST : Sends data of unlimited length to the Web server –PUT : Stores a resource under the request URL –DELETE : Removes the resource identified by the request URL –OPTIONS : Returns the HTTP methods the server supports –TRACE : Returns the header fields sent with the TRACE request

26 HTTP Support Javax.servlet.http.HttpServlet implements the javax.servlet.Servlet interface. HttpServlet methods: –Service(): Dispatches the HTTP message to one of severial special methods. –Others:Called by the server (via the service method) to allow a servlet to handle a GET, Post … request. doGet(): doDelete() doOptions() doPost() doTrace() To write an HTTP servlet, you can extend HttpServlet and add your own custom processing.

27 HTTP Support

28 Security Issues  The Servlet Sandbox  Access Control Lists (ACLs)

29 JSP Technology  IntroductionIntroduction  JSP ArchitectureJSP Architecture  JSP Access ModelsJSP Access Models  JSP Syntax BasicsJSP Syntax Basics

30 What Is a JSP Page? A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format, such as HTML, WML, and XML; and JSP elements, which construct dynamic content.

31 Components of JSP pages  Static HTML/XML components.  Special JSP tags  Snippets of code written in the Java programming language called "scriptlets."

32 JSP Advantages  Separation of static from dynamic content  Write Once Run Anywhere  Dynamic content can be served in a variety of formats  Recommended Web access layer for n-tier architecture  Completely leverages the Servlet API

33 JSP Architecture

34 JSP Architecture JSP pages are subject to a translation phase and a request processing phase.

35 …... public class _0005cjsp_0005cjsptest_0002ejspjsptest_jsp_0 extends HttpJspBase { … … public void _jspService(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { JspFactory _jspxFactory = null; … Date d = new Date(); String today = DateFormat.getDateInstance().format(d); // end // begin out.write("\r\nToday is: \r\n ");…... Translation

36 The Life Cycle of a JSP Page

37 JSP Access Models -1

38 JSP Access Models-2

39 JSP Syntax Basics  Directives  scripting elements  standard actions

40 Directives  tag.  two primary directives page include

41 scripting elements  Declarations, tag.  Expressions, tag  Scriptlets, tag  Comments, tag

42 Standard Actions  Using JavaBean Components <jsp:useBean id="user" class="com.jguru.Person" scope="session" />  Forwarding Requests  Including Requests

43 Java Bean JAVA BEANS components are Java classes that can be easily reused and composed together into applications. Any Java class that follows certain design conventions can be a JavaBeans component.

44 Java Bean Design Pattern The bean encapsulates its properties by declaring them private; –Read/write, read-only, or write-only –Simple, which means it contains a single value, or indexed, which means it represents an array of values The bean provides public accessor (getter/setter) methods for reading and modifying properties’ values. – For each readable property, the bean must have a method of the form PropertyClass getProperty() {... } – For each writable property, the bean must have a method of the form setProperty(PropertyClass pc) {... }

45 Java Bean Design Pattern In addition to the property methods, a JavaBeans component must define a constructor that takes no parameters.

46 An Example of Java Bean An Example of Java Bean public class Currency { private Locale locale; private double amount; public Currency() { locale = null; amount = 0.0; } public void setLocale(Locale l) { locale = l; } public void setAmount(double a) { amount = a; } public String getFormat() { NumberFormat nf = NumberFormat.getCurrencyInstance(locale); return nf.format(amount); }

47 Object Scope

48 Accessing Objects from a JSP Page

49 Servlets and JSP Pages  Can be used to solve identical problems.  Servlets are expressed in Java ; JSP pages are text-based documents  A servlet uses print statements to post HTML data Cannot preview the look and feel of an HTML page Difficult to maitain when data or its display format needs changes

50  JSP can use graphical development tools, use JavaBeans components and/or custom tags that handle complex dynamic formatting of HTML  Like servlets, JSP can work in a portable platform- or application-independent means and supports a reusable component model  A sophisticated Web application can consist solely of JSP pages, custom tags, and JavaBeans components  servlets are rarely necessary. Servlets and JSP Pages

51 When Servlet More Appropriate  Generating Binary Data  Extending a Web Server's Functionality  Best suited for low-level application functions not frequently modified  Communicating with Applets or applications