Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.

Slides:



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

 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
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.
Servlets Stoney Jackson
Servlets and a little bit of Web Services Russell Beale.
An introduction to Java Servlet Programming
Core Servlets Chapter 3 Link for Core Servlets code: om/archive/ om/archive/
2/16/2004 Dynamic Content February 16, /16/2004 Assignments Due – Message of the Day Part 1 Due – Reading and Warmup Work on Message of the Day.
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.
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.
Java Servlet Technology. Introduction Servlets are Java programs that run on a Web server, handle HTTP requests and build Web pages Servlet specification.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
Objectives Java Servlet Web Components
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
Chapter 5 Java Servlets. Objectives Explain the nature of a servlet and its operation Use the appropriate servlet methods in a web application Code the.
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.
Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time.
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 & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
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.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
S ERVLETS Hits Counter 21-Nov-15. S ERVLETS - H ITS C OUNTER Many times you would be interested in knowing total number of hits on a particular page of.
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.
@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.
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.
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.
Configuration Web Server Tomcat - Install JDK Install Tom cat Configure Tom cat for running Servlet C:\Program Files\Apache Software Foundation\Tomcat.
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.
 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,
J2EE/Java EE Apache Tomcat v9 IBM Websphere v8 Redhat JBoss EAP v6 Oracle Weblogic v12 Fusion Web Tier ServletsJSPJSF Business Tier JMXJMSEJB.
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.
Speaker Name Speaker Title Speaker Title Oracle Corporation Olivier Le Diouris Principal Product Manager Oracle Corporation Building Servlet and JSP Applications.
Distributed Web Systems Java Servlets Lecturer Department University.
CS3220 Web and Internet Programming Introduction to Java Servlets
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
HTTP Servlet Overview Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might.
Jagdish Gangolly State University of New York at Albany
Servlets and Java Server Pages
CS3220 Web and Internet Programming Introduction to Java Servlets
Introduction to Java Servlets
Basic servlet structure
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages

Java2 Enterprise Edition(J2EE) Framework Developed by SUN  open standard Offers a suite of software specification to design, develop, assemble and deploy enterprise applications  n-tier, Web-enabled, server-centric, component-based Provides a distributed, component-based, loosely coupled, reliable and secure, platform independent application environment.

J2EE Technologies J2SE JAX-RPC Web Service for J2EE J2EE Management J2EE Deployment JMX JMS JTA Servlet JSP EJB JAXR Connector JACC JAXP JavaMail JAF

What are Servlets ? Java technology for dynamic (web) content generation Run in a Servlet Container Receives requests from clients (web browsers), process them and generate response (html pages) that are sent back to clients

Why Build Web Pages Dynamically ? Static html web pages are very limited Dynamic generation is needed when for instance  The web page is based on data sent by the client search results login to personal page order page at online store...  The web page is derived from data that changes frequently weather page stock market quotes...  The web page uses information from databases or other server-side sources

Example of servlet import javax.servlet.*; import javax.servlet.http.*; import java.io.*; Public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(" First Servlet "); out.println(" Hello Code Camp! "); } Java code that produces html output

What does a servlet do ? Receives client request (mostly in the form of HTTP request) Extract some information from the request Do content generation or business logic process (possibly by accessing database, invoking EJBs, etc) Create and send response to client (mostly in the form of HTTP response) or forward the request to another servlet (or JSP page)

Servlet Containers Servlet runs in a “Servlet Container”  cooperates with a http server process  maps URLs to servlets  provides standard services (life cycle management, resource usage, deployment mechanisms, security, possibly transaction management) Several existing “Servlet Containers”  all need to satisfy a servlet container specification  examples: Apache Tomcat, Jetty, Redhat JBoss, BEA Weblogic, SUN Java System Web Server, IBM Websphere,...

Requests Carry information sent from a client (e.g. web browser) to a server The most common client requests HTTP GET & HTTP POST GET requests: User entered information is appended to the URL in a query string Can only send limited amount of data.../servlet/ViewCourse?FirstName=John&LastName=Wayne POST requests: User entered information is sent as data (not appended to URL) Can send any amount of data

Servlet Life Cycle Http request Http response Load Invoke No Yes ClientServer Is Servlet Loaded? Servlet Container Run Servlet

Servlet Life-Cycle methods Ready doGet( )doPost( ) destroy( )init( ) Request parameters Init parameters Methods invoked by Container defined in javax.servlet.http.HttpServlet and super classes Programming a servlet consists of overriding these methods

Servlet Life-Cycle methods init() Invoked once when the servlet is first instantiated Perform any set-up in this method destroy() Invoked before servlet instance is removed Perform any clean-up Closing a previously created database connection doGet(), doPost(), doXxx() Handles HTTP GET, POST, etc. requests Override these methods in your servlet to provide desired behavior

Servlet Life-Cycle methods - example import javax.servlet.*; import javax.servlet.http.*; public class LotteryNumbers extends HttpServlet { private long modTime; private int[] numbers = new int[10]; /** The init method is called only when the servlet * is first loaded, before the first request * is processed. */ public void init() throws ServletException { // Round to nearest second (i.e, 1000 milliseconds) modTime = System.currentTimeMillis()/1000*1000; for(int i=0; i<numbers.length; i++) { numbers[i] = randomNum(); }

/** Return the list of numbers that init computed. */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Your Lottery Numbers"; String docType = " \n"; out.println(docType + " \n" + " " + title + " \n" + " \n" + " " + title + " \n" + " Based upon extensive research of " + "astro-illogical trends, psychic farces, " + "and detailed statistical claptrap, " + "we have chosen the " + numbers.length + " best lottery numbers for you. " + " "); for(int i=0; i<numbers.length; i++) { out.println(" " + numbers[i]); } out.println(" "); }

/** The standard service method compares this date * against any date specified in the If-Modified-Since * request header. If the getLastModified date is * later or if there is no If-Modified-Since header, * the doGet method is called normally. But if the * getLastModified date is the same or earlier, * the service method sends back a 304 (Not Modified) * response and does not call doGet. * The browser should use its cached version of * the page in such a case. */ public long getLastModified(HttpServletRequest request) { return(modTime); } // A random int from 0 to 99. private int randomNum() { return((int)(Math.random() * 100)); }

Example - result

Handling Form Data Forms are used to submit request through web pages  data is send using GET or POST  data received as request parameters by Servlets

Handling Form Data A request can come with any number of parameters Parameters are sent from HTML forms: GET: as a query string, appended to a URL POST: as encoded POST data, not appeared in the URL getParameter("paraName") Returns the value of paraName Returns null if no such parameter is present Works identically for GET and POST requests

Handling Form Data Client Information Form Client id Client name Client birthdate

public class LookupServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... // get value of id field String id = request.getParameter("id"); // get value of name field String name = request.getParameter("name"); // get value of birthdate file Date birthdate = request.getParameter("birthdate");... // check if lookup is pressed if (request.getParameter("lookup" != null)) { // do whatever should be done } // check if add is pressed if (request.getParameter("add" != null)) { // do whatever should be done }