Servlets and Java Server Pages Object-Oriented Programming V22.0470 Written by: Haytham Allos (Instructor) New York University (NYU)

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.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
CGI programming. Common Gateway Interface interface between web server and other programs (cgi scripts) information passed as environment variables passed.
M-V-C for web applications. Model for Web Applications model consists of data and system state database tables –persistent data session information –current.
Lecture plan Information retrieval (from week 11)
Apache Tomcat as a container for Servlets and JSP
Java Server Pages (JSP)
Java Servlets Java Server Pages (JSP)
 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.
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.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
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.
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.
Indy Java User’s Group March 27, 2001 Sallie Mae 6:00p.m.
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.
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
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
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.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
Standalone Java Application vs. Java Web Application
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.
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.
Chapter 6 Server-side Programming: Java Servlets
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.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
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.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
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
 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,
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Chapter 4 Request and Response. Servlets are controlled by the container.
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.
JSP / Servlets and Beans
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
Speaker Name Speaker Title Speaker Title Oracle Corporation Olivier Le Diouris Principal Product Manager Oracle Corporation Building Servlet and JSP Applications.
CS122B: Projects in Databases and Web Applications Spring 2017
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Winter 2017
Servlet Fudamentals.
MSIS 655 Advanced Business Applications Programming
CS122B: Projects in Databases and Web Applications Winter 2018
CS122B: Projects in Databases and Web Applications Spring 2018
J2EE Lecture 1:Servlet and JSP
CS122B: Projects in Databases and Web Applications Winter 2019
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Servlets and Java Server Pages Object-Oriented Programming V Written by: Haytham Allos (Instructor) New York University (NYU)

Static content Web Server delivers contents of a file (html)Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML to Browser 2. Web Server reads file from disk browser Web Server

Dynamic Content CGI( Common Gateway Interface )program generates HTML that is returned to BrowserCGI( Common Gateway Interface )program generates HTML that is returned to Browser 1. Browser sends request to Web Server 5. Web Server sends HTML to Browser User Computer Server 2. Web Server loads CGI program from disk CGI Program 3. Web Server starts CGI program 4. CGI program generates and returns HTML browser Web Server

CGI has issues performanceperformance –Web Server must create new process for each request, limits scalability maintenancemaintenance –presentation and business logic tightly coupled

Alternatives FastCGI - persistent processFastCGI - persistent process mod_perl - perl interpreter embedded in Apache web servermod_perl - perl interpreter embedded in Apache web server Server Extensions - Netscape(NSAPI), Microsoft(ISAPI)Server Extensions - Netscape(NSAPI), Microsoft(ISAPI) Active Server PagesActive Server Pages

Java Servlets replaces CGIreplaces CGI a Java programa Java program runs in Servlet Container or Engineruns in Servlet Container or Engine generates dynamic content(HTML, XML)generates dynamic content(HTML, XML) now part of J2EE architecturenow part of J2EE architecture good performancegood performance

Java Servlets Continued... AdvantagesAdvantages –generally faster, more efficient than CGI – runs as a thread not an OS process –Convenience - Java API’s –secure - does not run in a shell –portable, vendor independent

Java Servlet Diagram Extends Web Server Extends Web Server 1. Browser sends request to Web Server 6. Web Server sends HTML to Browser 2. Web Server sends request to Servlet Engine Servlet 5. Servlet generates and returns HTML browser Web Server Servlet Container DB TopLink 4. Servlet can access database jdbc 3. Servlet Engine runs the servlet

Simple Servlet public class HelloWorld extends HttpServlet { public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response. setContentType(" text/ html"); PrintWriter out = response. getWriter(); out. println( " " ); out. println( " Hello World ”); out. println( " ”); out. println( " Hello World ”); out. println( " "); }

Servlet Issues servlet generating HTML results in presentation and business logic tightly coupled with accompanying maintenance issues.servlet generating HTML results in presentation and business logic tightly coupled with accompanying maintenance issues.

Java Server Pages(JSP) Built on Servlet technologyBuilt on Servlet technology simplified way to create pages containing dynamically generated content.simplified way to create pages containing dynamically generated content. converted into servlet, compiled and run as a normal servletconverted into servlet, compiled and run as a normal servlet timestamp checked at specified interval, regenerated if necessary.timestamp checked at specified interval, regenerated if necessary.

Java Server Pages(JSP) continued... jsp page contains jsp page contains –directives, comments, fixed template data (HTML, XML), jsp expression or tags, scriptlets

Java Server Pages(JSP) continued... Can be invoked from a browser via URL or from a ServletCan be invoked from a browser via URL or from a Servlet Java code can be in the jsp sourceJava code can be in the jsp source –keep simple Java code can be in a Java BeanJava code can be in a Java Bean –separates User Interface from Business Logic –Used via UseBean tag

Java Server Page Diagram 1. Browser sends request to Web Server 6. Web Server sends HTML to Browser 2. Web Server sends request to Servlet Engine Servlet 6. JSP(Servlet) returns HTML browser Web Server Servlet Container DB TopLink 5. JSP interacts with Java Bean(s) and/or Servlets jdbc 4. Servlet Container runs the JSP (Servlet) jsp JSP Engine Servlet source code Java Compiler JSP (Servlet) Java Bean 3. JSP Engine generates servlet

Simple Java Server Page Hello World. You are using a computer called !

MVC Architecture Model: data (java bean)Model: data (java bean) View: presentation / user interface (jsp)View: presentation / user interface (jsp) Controller: directs things / traffic cop (servlet)Controller: directs things / traffic cop (servlet) Browser Servlet(controller) JSP(View)Java Bean(Model) DB Servlet Container request response 1 instantiate

J2EE Architecture

Servlet/JSP specifications Servlet 2.0 JSP 1.0Servlet 2.0 JSP 1.0 Servlet 2.2 JSP 1.1Servlet 2.2 JSP 1.1 Servlet 2.3 JSP 1.2Servlet 2.3 JSP 1.2 –Draft stage –J2EE

Resources BooksBooks –O’Reilly - –Prentice Hall - –Addison-Wesley -

Servlet/JSP Fundamentals Servlets like any other Java class, except:Servlets like any other Java class, except: Must extend HttpServletMust extend HttpServlet Must contain a doPost or doGet methodMust contain a doPost or doGet method Come equipped to handle the laborious points of httpCome equipped to handle the laborious points of http Http requests and responses are made into Java objectsHttp requests and responses are made into Java objects Best alternative to traditional CGIBest alternative to traditional CGI

Servlet/JSP Fundamentals Anatomy of a ServletAnatomy of a Servlet –Optional init method, executes the first time a servlet is invoked –doGet/doPost methods execute when request is made –method signature accepts http request and response as parameters –Optional destroy method – Samples Sampleswww.servlets.com Samples

Servlet/JSP Fundamentals One instance of Servlet for each Servlet nameOne instance of Servlet for each Servlet name Same instance serves all requests to that nameSame instance serves all requests to that name Instance members persist across all requests to that name.Instance members persist across all requests to that name. Local /block variables in doPost & doGet are unique to each requestLocal /block variables in doPost & doGet are unique to each request

JSP as presentation model Servlet Performs the hard work Clientrequest JSPSuccessfulresults Helper objects Persistent “bean” objects DB Connections, etc JSPUnsuccessful results results App logic Presentation Subsequent client client request request Simple calls to bean properties No servlet need if bean still set

JSP alternatives JSP can still result in unwieldy code creeping into HTMLJSP can still result in unwieldy code creeping into HTML Most HTML editors don’t like itMost HTML editors don’t like it JSP:Taglibs and other “templating” programs may help.JSP:Taglibs and other “templating” programs may help. Much depends on how HTML documents are produced and what business logic you are presentingMuch depends on how HTML documents are produced and what business logic you are presenting

In Depth Examples Encapsulation of SQL callsEncapsulation of SQL calls List results and view thread detailList results and view thread detail Post reply and add to favoritesPost reply and add to favorites Set all bean properties w/ requestSet all bean properties w/ request Edit usersEdit users Query centralQuery central

Pitfalls, surprises, and work- arounds HTTP works via requests and responses. Client caching of responses poses a problem.HTTP works via requests and responses. Client caching of responses poses a problem. Guard against evil use of the “back” buttonGuard against evil use of the “back” button Don’t assume everyone’s client will respond properly to the “defaults”Don’t assume everyone’s client will respond properly to the “defaults” Guard against easy spoofs of any GET request which alters data.Guard against easy spoofs of any GET request which alters data.