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.

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.
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.
Servlets and a little bit of Web Services Russell Beale.
An introduction to Java Servlet Programming
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.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
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.
Advanced Java Programming Unit Two: Servers and Servlets Gareth Lee Department of Electrical and Electronic Engineering, University of Western Australia.
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.
SE-2840 Dr. Mark L. Hornick1 Java Servlet-based web apps Servlet Architecture.
CSCI 6962: Server-side Design and Programming History and Background.
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
Server Side Scripting Norman White. Where do we do processing? Client side – Javascript (embed code in html) – Java applets (send java program to run.
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)
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)
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.
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.
Java Servlets & Java Server Pages Lecture July 2013.
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)
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.
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.
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
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.
S ERVLETS Form Data 19-Mar-16. F ORM P ROCESSING You must have come across many situations when you need to pass some information from your browser to.
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.
Servlets and JSP 20-Nov-18 servletsJSP.ppt.
CS122B: Projects in Databases and Web Applications Winter 2018
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
Presentation transcript:

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 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

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 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 information 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 response document that is returned to the user Each has a raft of methods so check the Javadoc for details

Example: a simple chat server A 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 some client side technologies when possible  This places the load on the client machines rather than the server

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 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