Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.

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.
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.
An introduction to Java Servlet Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 10 Object Oriented Programming in Java Advanced Topics Servlets.
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.
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
JSP/Servlets. Web Servers What is a Web Server?  A server program that listens on a standard port and handles http protocol.  http protocol consists.
Servlets Replace Common Gateway Interface Scripts Extend Server Functionality Modules (software components) Like applets to browsers No GUI.
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.
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 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.
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.
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 Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
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.
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.
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.
L.MARIA MICHAEL VISUWASAM UNIT-4
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.
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.
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.
Distributed Web Systems Java Servlets Lecturer Department University.
Introduction to Servlets
Servlets.
Servlet Fudamentals.
Net-centric Computing
JDBC & Servlet CSE 4504/6504 Lab.
HTTP Servlet Overview Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might.
Java Servlets 9/21/2018.
Chapter 26 Servlets.
Servlets and 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.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Basic servlet structure
Presentation transcript:

Servlets

Our Project 3-tier application Develop our own multi-threaded server Socket level communication

Web Application Shift Perspective use web server Develop servlet to extend web server to allow dynamic contents

Web Server listens on a standard port and handles http protocol. A browser requests for documents (mainly html files) + upload of small amounts of data Two most important http protocol elements: (hidden from you by browser) – GET (request document, may upload data) – POST (request document, upload data). 4

Early Web Servers Early web server were static, acted more like file servers: – Browser requests page – Server hands over page – Browser interprets html and displays to user – Might contain gif or jpeg images or simple animations 5

Modern Web Servers Need to support dynamic page E-Commerce became popular Need web pages to act more like programs that could interact with user. 6

Running an Applet 7 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Running A Servlet Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 8

Servlets Java’s answer to CGI, – very simple – Relatively high-level Requirements: a servlet-enabled web server When specified by your web page, web page passes http requests to java method (assuming everything is setup properly) 9

What do Servlets do? Read data sent by the user (e.g., form data) Look up other information about request in the HTTP request (e.g., headers, cookies, etc.) Generate the results (may do this by talking to a database, file system, etc.) Format the results in a document (e.g., make it into HTML Set the appropriate HTTP response parameters (e.g., cookies, content-type, etc.) Send the document to the user 10

Supporting Servlets The Web server must support servlets (since it must run the servlets): – Apache Tomcat – Sun’s JavaServer Web Development Kit (JSWDK) – Other web servers 11

Writing Servlets, cont. All servlets extend the Servlet class. All http servlets (by far most typical) should extend the HttpServlet class. In extending HttpServlet, you typically override the following methods: – init, service or doGet/doPost, destroy (very common) – doPut, doDelete, doOptions, doTrace (rare) Note: there is NO main() for Servlets! 12

Main HttpServlet Methods init() – called once when servlet is loaded by server. Contains any initializations that are common to all requests. doGet(HttpServletRequest, HttpServletResponse) – Called each time the servlet receives an http GET request posted by a client. Passes two objects, one representing the information of the request, the other used to configure a response. We’ll study these methods soon. 13

Main HttpServlet Methods, cont. doPost(HttpServletRequest, HttpServletResponse) – Same as doGet but for an http POST request. destroy() – Called before servlet is unloaded from memory. Performs any final cleanup, freeing memory, closing connections, etc. 14

Service Method Important: The method service(HttpServletRequest, HttpServletResponse) is also called for each servlet invocation. Service() in turn calls doGet and doPost, etc. for an HttpServlet. It is best not to override service even if you want to handle doGet and doPost identically. Simply have one call the other. 15

HttpServletRequest Object Passed when browser calls doGet and doPost. Most import methods for beginning servlet programming (in HttpServletRequest class): – String getParameter(String paramName) – String[] getParameterNames() – String[] getParameterValues() Makes getting data from web pages very simple. Many other methods for images, cookies, etc. 16

HttpServletResponse Object Passed when browser calls doGet or doPost Most import methods for beginning servlet programming: – PrintWriter getWriter(); Get Writer for communicating back to client – setContentType(String); Typically use “text/html”, indicating that html will be sent back to the browser 17

Examples import javax.servlet.http.*; import javax.servlet.*; import java.io.*; public class SimpleServlet extends HttpServlet { /*Handle the HTTP GET method by building a simple web page.*/ public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriterout; Stringtitle = "Simple Servlet Output”; response.setContentType("text/html"); // set content type out = response.getWriter(); // then write the data of the response out.println(" "); out.println(title); out.println(" "); out.println(" " + title + " "); out.println(" This is output from SimpleServlet."); out.println(" "); out.close(); }

The Page Test Simple Servelet Example Test Simple Servelet Example