Advanced Java Programming Unit Two: Servers and Servlets Gareth Lee Department of Electrical and Electronic Engineering, University of Western Australia.

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
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.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Servlets and a little bit of Web Services Russell Beale.
An introduction to Java Servlet Programming
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
1 CS6320 – Why Servlets? L. Grewe 2 What is a Servlet? Servlets are Java programs that can be run dynamically from a Web Server Servlets are Java programs.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
1 Java Server Pages Can web pages be created specially for each user? What part does Java play?
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.
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. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
SE-2840 Dr. Mark L. Hornick1 Java Servlet-based web apps Servlet Architecture.
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.
Applets & Servlets.
1 Servlet How can a HTML page, displayed using a browser, cause a program on a server to be executed?
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
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.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
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.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
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. Servlets When we run small Java programs within a browser these are referred to as Applets... And when we run small Java programs within.
1 Streams Files are a computer’s long term memory Need ability for programs to –get information from files –copy information from program variables to.
Chapter 6 Server-side Programming: Java Servlets
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
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)
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
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.
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.
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.
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
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.
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.
CS122B: Projects in Databases and Web Applications Spring 2017
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Winter 2017
Introduction Servlets and JSP
Advanced Java Programming Unit Two: Servers and Servlets
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Chapter 26 Servlets.
Clients and Servers 19-Nov-18.
CS122B: Projects in Databases and Web Applications Winter 2018
Clients and Servers 1-Dec-18.
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.
CS122B: Projects in Databases and Web Applications Winter 2019
Clients and Servers 19-Jul-19.
Clients and Servers 13-Sep-19.
Presentation transcript:

Advanced Java Programming Unit Two: Servers and Servlets Gareth Lee Department of Electrical and Electronic Engineering, University of Western Australia John Morris Gareth Lee

Overview To create a simple HTTP server in Java To use the implementation to illustrate a number of advanced Java features: TCP/IP Sockets and Server Sockets Interfaces Software components (more from John later) Multithreading To show how to create executable server objects (using Sun’s Servlets API)

Recommended Reading Java Network Programming, Elliotte Rusty Harold, O’Reilly and Associates, 1997, ISBN TCP/IP Network Administration, Second Edition, Craig Hunt, O’Reilly and Associates, 1997, ISBN The Java Developer’s connection: The Javadoc documentation

(Pseudo) requirements Server must be able to process HTTP/1.0 file transfer requests and deliver files Connections are to be made via TCP/IP Must be efficient and prompt Must be simple to understand and elegant in design

HTTP protocol Developed by Tim Berners-Lee at CERN Like most Internet protocols it is described in an RFC (Request for Comment document): RFC1945 May be downloaded from the Internet Engineering Task Force’s web site:

Back to server example Some of you may have covered this in the introductory Java course Servers have a listener loop Loop until the server is shutdown Wait for a client to request a connection Read the details of the client’s request Provide the requested information to the client Here’s the listener loop from our example:

HttpServer ServerSocket socket = new ServerSocket(80, 5); public void listen() throws IllegalAccessException, InstantiationException, IOException { for (;;) { System.err.println("HttpServer: waiting..."); Socket s = socket.accept(); FileServer f = createFileServer(); f.dispatch(s); }

How it all fits together Client (sid)Server (fred) ServerSocket ss. s = ss.accept() s = new Socket (“fred”, 80) Socket s s.getInputStream() s.getOuputStream() s.getInputStream() s.getOuputStream()

…but more of that later! Good software is designed in a modular fashion avoiding stovepipe designs! This is a form of software components Java has strong support for components Components hide their implementation behind interfaces An interface defines a contract between the supplier/server and the user/client.

How clients use interfaces ServerSocket socket = new ServerSocket(80, 5); public void listen() throws IllegalAccessException, InstantiationException, IOException { for (;;) { System.err.println("HttpServer: waiting..."); Socket s = socket.accept(); FileServer f = createFileServer(); f.dispatch(s); }

Interfaces benefit clients Simplifies client implementation Clients do not need to worry about the implementation details Interfaces encapsulate state of different subsystems  side effects reduced Define clear boundaries between different teams of programmers Clients can substitute alternative implementations: polymorphism Clients can purchase off the shelf solutions: software components

Interfaces simplify clients Software Component Client Program Interface /ublic class HttpServer { /** Listens indefinitely for transfer requests and creates a server instance for each request. */ public void listen() throws IllegalAccessException, InstantiationException, IOException { for (;;) { /* Block, waiting for a request to occur then spawns a new (anonymous) socket with which to deal with the request. */ System.err.println("HttpServer: waiting..."); Socket s = socket.accept(); /* Create a file server to deal with the new socket. */ FileServer f = createFileServer(); f.dispatch(s); } public static void main(String[] args) { try { HttpServer htts = new HttpServer("sea.server.ThreadedFileServer"); htts.listen(); } catch (Exception e) { System.err.println("HttpServer: failed due to exception:\n" + e); }

The FileServer interface public interface FileServer { /** This method allows an incoming HTTP request to initiate a file dispatch. The socket will provide an input stream (which is at the beginning) from which an HTTP/1.0 header request may be read. It also provides an output stream on which the request should be delivered. The delivery should have an HTTP/1.0 header s The socket on which a request is being made. Once this method has returned the socket will have been closed by the dispatcher. */ public void dispatch(Socket s); }

Interfaces are contracts Each interface is a contract between two parties The contract should be made as strict and precise as possible Avoid unnecessary ambiguity Document the contract within the interface’s source file using Javadoc

Implementing FileServer Two flavours of FileServer have been provided using deferred instantiation A simple one but with low performance: sea.server.SimpleFileServer A server that uses multiple threads to increase performance: sea.server.ThreadedFileServer A server which uses a pool of threads to achieve the maximum possible performance: sea.server.ThreadedServer2

SimpleFileServer (1) Must implement the FileServer interface so that it can plug in to the HttpServer Reads the HTTP request from the Socket’s input stream Decides which file is required Reads the file and spools to the Socket’s output stream.

SimpleFileServer (2) public class SimpleFileServer implements FileServer { protected Socket s = null; public void dispatch(Socket s) { this.s = s; respond(); }.... }

SimpleFileServer (3) Socket s; InputStream inStream = s.getInputStream(); InputStreamReader reader = new InputStreamReader(inStream); BufferedReader input = new BufferedReader(reader); Must get an input stream so that we can analyse the request Socket provides the method InputStream getInputStream();

SimpleFileServer (4) Request consists of a number of lines of text separated by “\r\n” First line is all this server is interested in A typical request might be of the form: GET /path/to/file.html HTTP/1.0 Accept: text/html Accept: image/gif User-Agent: Lynx/2.4

SimpleFileServer (5) Cuts out the file name Looks for the file relative to the current working directory (not portable!!) If the file is a directory look for the file “index.html” in the directory If the file does not exist then respond with an error (code 404)

SimpleFileServer (6) Must construct a header for the response Code 200 means success Simple header takes the following form: HTTP/ OK Server: SEA/1.0 MIME-version: 1.0 Content-type: text/html Data starts after blank line... More data, etc...

SimpleFileServer (7) Get the output stream from the Socket OutputStream getOutputStream() Spool (copy) the file contents into the socket If the MIME type is textual then we must make sure the lines are delimited by “\r\n”. Otherwise we pass the file unmodified

Server performance (1) The SimpleFileServer is completely sequential. It handles one request at a time. Reading a file from disk takes a long time (around 10ms) The server will be sitting idle while it waits for the file to load (wasting up to 10 6 instruction cycles) Other web browsers will be kept waiting

Server performance (2) Start HTTP request loading Block awaiting disk availability Deliver web page across network time

Threaded servers (1) Threaded servers can process several requests at once. Each request is handled by a separate thread. This doesn’t increase the overall amount of work done (unless using SMP)... but it does reduce the wastage! Threaded operation is worthwhile when threads are expected to block, awaiting I/O operations

Threaded servers (2) Start HTTP request loading Block awaiting disk availability Deliver web page across network time

Threaded FileServer (1) Java provides very convenient multithreading to programmers We can add threads using inheritance We can supplement the existing capabilities of the SimpleFileServer class We create a class ThreadedFileServer which extends the existing SimpleFileServer You may have covered threads in the Introductory Java Course

Threaded FileServer (2) public class ThreadedFileServer extends SimpleFileServer implements FileServer, Runnable { private static int index = 0; public void dispatch(Socket s) { super.s = s; Thread thread = new Thread(this, ”Server-" + (index++)); thread.start(); } public void run() { super.respond(); }

java.lang.Thread (1) Creates new threads within the virtual machine Classes which start threads must implement interface java.lang.Runnable interface Runnable { /** This is the method that will be run when the new thread is started. */ public void run(); }

java.lang.Thread (2) Must create a Thread object associated with each new thread using the constructor Thread(Runnable run, String threadName) Start a thread with the method void start() Other useful methods can be used to set priorities and interrupt a running thread

java.lang.Thread (3) Our threads do not share any common memory locations (except for index) When threads read/write a shared memory area access must be synchronized Otherwise it is impossible to predict how the system will behave Java has mechanisms for achieving this

But even threads cost Starting a thread can be relatively expensive when performance is critical Our threaded server creates a new Thread for each file to be transferred A better approach is to create a pool of threads and recycle them Create a pool of threads which are ready to work when needed Have threads wait until work is available Better, but more complex so look at the class sea.server.ThreadedFileServer2

Making the server programmable Our example web server performs a very simple task Accept a request from a client Retrieve the appropriate document from disk Return the document to the client This is too limiting How do we implement searches? We need to be able to run programs within the server to process user requests Accept a client request including arguments Run a program on the arguments Return results in the form of a document

Servlets When we run small Java programs within a browser these are referred to as Applets... so we run small Java programs within a server these are “Servlets” A servlet is a program designed to process a client request (which requires interactivity). It processes arguments and formats its results as a short lived document. HTML servlets are becoming a popular mechanism for creating interactive servers.

Servlets versus CGI (1) Traditionally programs were run on web servers using Common Gateway Interface (CGI) scripts written in languages such as Perl. Must create a new interpreter process for each client request Comparatively slow to start Expensive of memory resources when serving several clients at the same time Interpreted programs are CPU intensive

Servlets versus CGI (2) Servlets use Java objects which persist between requests to the server Low latency since requests run in threads Offer performance advantages since programs are compiled and can take advantage of JITs and/or Hotspot JVMs. Servlet groups can share a JVM leading to smaller memory footprints. Servlets run in a Sandbox offering protection from malicious (or accidental) damage Programs are future proofed since WORA offers better scope for server upgrades.

Creating a simple servlet Servlets are written in a similar fashion to applets Write a new servlet class which extends javax.servlet.http.HttpServlet (or just implements javax.servlet.Servlet ) Override certain methods to deal with requests Get your methods to create an HTML document to return information to the client’s browser Load the servlet byte codes onto your web server (for example apache/jserv)

Import servlet methods (1) When the servlet is first loaded it makes a single call to the method public void init(ServletConfig config) This may optionally be overridden to initialise the state of the servlet (for example loading state information from a file). When a servlet is finally unloaded it makes a single call to the method public void destroy() If you wish to save to servlet state to a file (or using JDBC) this is the method to override

Import servlet methods (2) To handle an HTTP GET request implement protected void doGet(HttpServletRequest request, HttpServletResponse response) If a browser visits your servlet this is where you get to create a document for it to display To handle an HTTP POST request provide protected void doPost(HttpServletRequest request, HttpServletResponse response) If your document contains an HTML form and the user posts the results this is where you can extract and process them Also methods for HTTP OPTIONS, TRACE and DELETE (more exotic options)

Import servlet methods (3) Two objects are passed as parameters to all these handler methods: javax.servlet.http.HttpServletRequest Represents the formation that was passed to the server when the user submitted the request by visiting/posting to the servlets URL. javax.servlet.http.HttpServletResponse Used to construct a reponse document that is returned to the user Each has a raft of methods so check the Javadoc for details

A simple chat server An web based chat room server A number of users can connect to the servlet using browsers Read a list of the previous messages Optionally append new messages to the list Messages are attributed to a specific author and are time stamped Messages do not persist after the chat server is stopped (easy enough to rectify)

ChatServer (1) public class ChatServlet extends HttpServlet { Vector messages = new Vector(); public void init(ServletConfig config) throws ServletException { super.init(config); } public void destroy() { // Currently does nothing }.... }

ChatServer (2) protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { createDocument(response); } protected void createDocument(HttpServletResponse response) throws IOException { response.setContentType("text/html"); response.setHeader("pragma", "no-cache"); PrintWriter writer = response.getWriter(); writer.println(" "); writer.println(" Chat Servlet "); writer.println(" "); Date now = new Date(); writer.println("Current server time is " + now + " ");.... writer.println(" "); writer.close(); }

ChatServer (3) for (int i = 0; i < messages.size(); i++) { writer.println(" "); String messageString = (String) messages.elementAt(i); writer.println(messageString); } writer.println(" "); writer.println("Enter your name: “ + “ "); writer.println("Enter your message: ” + “ ” + “Type your message here "); writer.println( " "); writer.println(" ");

ChatServer (4) protected synchronized void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String message = request.getParameter("message"); if (name != null && message != null) { Date timeStamp = new Date(); String messageString = " Message " + messages.size() + " from " + name + " at " + timeStamp + ": " + message + " "; messages.add(messageString); } createDocument(response); }

Performance (1) Servlets offer better performance than most of the previous CGI like technologies But CGI/Servlets concentrate the load on the server When designing high throughput servers only use servlets where you really need interactivity Searches/Shopping carts Data that is very short lived (stock quotes) This also applies to low throughput servers that might need to scale later

Performance (2) Consider using periodic programs to generate static documents on disk The cost of serving fixed documents will always be less than the cost of server side execution Disk space is cheap! Consider using applets when possible This places the load on the client machines rather than the server Finally consider using SMP and/or server farms Complex and very expensive

Pull versus Push transports How can a chat reader find out when a new message has been posted by another author? Only by repeatedly hitting the Reload button! HTTP (& TCP/IP services in general) transfer documents on the user’s request To push updates automatically from the server you will need to: Start a reverse server within each client Use a multicast group Use a remote procedure call system such as RMI or CORBA

Servlets and JSP Java Server Pages is an extension to the servlets API. With conventional servlets you embed the HTML that you need inside a Java program. With JSP you embed your Java program within a HTML document (by using special tags). Works rather like JavaScript but the JSP script runs on the server before the page is dispatched to the user’s browser.

Useful sources of information For information about HTML try You can download Sun’s servlet development kit from their web site at the You can download apache’s Tomcat server from For other information about Servlet development try

Homework Read through the sample code to convince yourself you understand what’s going on Sample code can be downloaded from Read the code documentation If you can, run the examples to check they work

Comments, Suggestions... How was the presentation paced? Was there enough (or too much) technical content? Any areas of particular interest? Comments regarding presentation style?