Distributed Web Systems Java Servlets Lecturer Department University.

Slides:



Advertisements
Similar presentations
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Advertisements

Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
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.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
Core Servlets Chapter 3 Link for Core Servlets code: om/archive/ om/archive/
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Java Server and Servlet CS616 Team 9 Kim Doyle, Susan Kroha, Arunima Palchowdhury, Wei Xu.
Server Side Programming Web Information Systems 2012.
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 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.
A Servlet’s Job Read explicit data sent by client (form data) Read implicit data sent by client (request headers) Generate the results Send the explicit.
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.
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.
Introduction Servlets and JSP Celsina Bignoli
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
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.
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
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 Lec 27. Creating a Simple Web Application in Tomcat.
1 CS122B: Projects in Databases and Web Applications Spring 2015 Notes 03: Web-App Architectures Professor Chen Li Department of Computer Science CS122B.
1 MSCS 237 Overview of web technologies (A specific type of distributed systems)
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.
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.
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.
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.
Java Servlets and Java Server Pages
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.
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.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
CS122B: Projects in Databases and Web Applications Spring 2017
Introduction to Servlets
CS3220 Web and Internet Programming Introduction to Java Servlets
CS122B: Projects in Databases and Web Applications Winter 2017
Introduction Servlets and JSP
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Net-centric Computing
Chapter 26 Servlets.
CS122B: Projects in Databases and Web Applications Winter 2018
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Spring 2018
CS3220 Web and Internet Programming Introduction to Java Servlets
CS122B: Projects in Databases and Web Applications Winter 2019
Basic servlet structure
Presentation transcript:

Distributed Web Systems Java Servlets Lecturer Department University

Outline Motivation Previous technologies Java servlets, advantages Details

Example application scenario Booking flights on-line: Airline web server User’s PC Airline reservations database Hotel reservation server VISA payment processing

Motivation Have to build Web pages dynamically: –Web page is based on data sent by the client E.g. your travel dates and preferences –Web page is derived from data that changes frequently E.g. seats availability, special hotel offers –Web page uses information from corporate databases or other server-side sources Reservations databases, banking systems, etc

Overall idea Simply put, writing servlets and JSPs is about making programs that make Web pages: call a program and pass it request details return web page (HTML) servlet request a web page return HTML Web server User’s PC Server-side resources call necessary services, obtain necessary data

Previous technologies CGI (common gateway interface) –One of the most common of the server-side web technologies –A CGI program can be written in just about any language, though the most popular language for CGI programming is Perl –Web servers as a gateway between the user request and the data that it requires: create a new process in which the program will be run load any required runtime environments as well as the program itself Finally, pass in a request object and invoke the program –When the program is finished, the web server will read the response from standard output

Previous technologies (contd.) ASP.NET (active server pages): –Microsoft production: combines HTML, scripting, and server-side components in one file called an Active Server Page (ASP) –When the server receives a request for an ASP file, it will first look for the compiled page and then execute it –If the file has not yet been compiled, the server will go ahead and compile and run it –An Active Server Page can be written using HTML, JScript, and VBScript –Through scripting, the Active Server Page can access server-side components –These components can be written in any language as long as it presents a COM (Microsoft's component specification) interface

Problems Doesn't scale well (CGI) –Each time a request is received by the web server, an entire new process is created Platform-dependent (ASP) –Can only be used with Microsoft web servers –There are ports to other platforms and web servers, but the lack of wide COM support reduces their effectiveness Maintenance nightmare (ASP) –mix of script and HTML, basically two sets of information threaded together

Java Servlets … and then the Sun shone –Servlet – a Java program executed on the Web server (within a servlet container) Servlet’s job: –Read explicit data sent by client (form data) –Read implicit data sent by client (request headers) –Generate the results –Send the explicit data back to client (HTML) –Send the implicit data to client (status codes and response headers)

Advantages of servlets Efficient –Threads instead of OS processes, one servlet copy, persistence Convenient –Lots of high-level utilities Powerful –Sharing data, pooling, persistence Portable –Run on virtually all operating systems and servers Secure –No shell escapes, no buffer overflows Inexpensive –There are plenty of free and low-cost servers.

Advantages of servlets (contd.) Supports the Model-View-Controller design pattern –Separate data (model) and user interface (view) concerns –Better suited for large-scale application development Also adopted by more recent web programming technologies, like Ruby on Rails –Next-generation web applications framework –Even shorter application development time using modern ideas like “scaffolding”, “convention over configuration”, “simple installation”, etc.

Web application Servlets: detailed scenario URL: Web server User’s PC Servlet container Web application servlet Web application Static web pages and other resources (images etc)

Recall HelloWorld in Java… import java.io.*; public class HelloWorld { public static void main(String args[]) { System.out.println(“Hello World !!!”) } This class links your program to the terminal (console)

Input/output in servlets Servlets communicate with servlet containers (and hence with the user) via two objects: –HttpServletRequest (for input data) –HttpServletResponse (for output data) HttpServletResponse User’s PC Servlet container Servlet HttpServletRequest PrintWriter

Basic servlet structure import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ServletTemplate extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Use "request" to read incoming HTTP headers // (e.g. cookies) and HTML form data (query data) // Use "response" to specify the HTTP response status // code and headers (e.g. the content type, cookies). PrintWriter out = response.getWriter(); // Use "out" to send content to browser } the actual logic is somewhere here

Now HelloWorld 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 { PrintWriter out = response.getWriter(); out.println("Hello World"); }

Really, should output HTML… public class HelloWWW extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(" \n" + " Hello WWW \n" + " \n" + " Hello WWW \n" + " "); } Always do this BEFORE printing out HTML!

Servlets: terminology Servlet = Java program Servlet container –software responsible for providing execution environment for servlets (forward requests to the appropriate servlets, get back the results, etc) –Examples: Caucho Resin, Macromedia JRun, Apache Tomcat Web application –a set of related servlets, JSPs, and other resources like HTML pages, images, etc Context path –part of the URL that tells the servlet container which Web application to forward the request to (e.g. “/myapp” in our example below) –

Installing Web applications How to install a new Web application into a servlet container? Need to specify three things: –What servlets, JSPs and other files it consists of –What context path the container should use for it –Mapping – how to map URLs to particular servlets within application, e.g. specify that / /my/great/servlet should call servlet class HelloWorld mapping

Servlet life-cycle Basically, the lifecycle of a servlet is as follows: –The servlet container creates an instance of the servlet –The container calls the instance's init( ) method –If the container has a request for the servlet, it creates a new thread and calls the instance's service( ) method in that thread –Before destroying the instance, the container calls its destroy( ) method –The instance is destroyed and marked for garbage collection And that's it! (Well, actually for efficiency reasons containers usually do not create a thread for each new request, but have a pool of threads to use…)

The service() method Dispatcher of HTTP requests: –Determines the type of request (GET, POST, HEAD, OPTIONS, DELETE, PUT or TRACE) and calls the associated method: doGet( ), doPost( ), doHead( ), doOptions( ), doDelete( ), doPut( ) or doTrace( ) Overwrite the doXxx() methods as needed, NOT the service() method! Input parameters are the same for all doXxx(): –HttpServletRequest request, HttpServletResponse response Standard (not overridden) doXxx() methods will return an HTTP error saying that that method is not valid for this resource

doGet() and doPost() Since GET and POST are the main types of HTTP requests, normally need to worry only about –doGet(): processes GET requests –doPost(): processes POST requests What’s the difference? –the way input request parameters are passed (more in the “Handling client request & response data” lecture!) If want to do the same for both, call doGet() from doPost() (or vice versa)

Debugging servlets Unfortunately, unavoidable for sizeable complex applications Where to get more debugging information? –User debugging print statements –Look at the HTML source (use “View Source” in the browser) –Use the container’s log files –Return error pages to the client – plan ahead for missing or malformed data –Look at the request data separately (see EchoServer at

Summary Servlets – yet another technology for generating dynamic Web pages –But, with some advantages over CGI, ASP, PHP, etc Servlet = Java program that –Receives HTTP request –… –Prints out HTML in HTTP response Questions