Servlet and JSP Programming: An Introduction Spiros Papadimitriou

Slides:



Advertisements
Similar presentations
Java Server Pages (JSP)
Advertisements

JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
Java Servlet & JSP © copyright 2005 SNU OOPSLA Lab.
Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.
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
CSE 190: Internet E-Commerce Lecture 7. HTML Templates Designed to separate server side logic from HTML presentation Key features –Escapes from HTML into.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
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.
Java Server Pages by Jon Pearce. JSP Documents JSP docs are XHTML Documents containing: –Fixed-Template Data: FTD HTML Components XML markup –JSP Components:
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 10 Object Oriented Programming in Java Advanced Topics Servlets.
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.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems JavaServer Pages (JSP)
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.
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
Servlets Pranav Maydeo. What is a Servlet ? Servlets are modules of Java code that run in a server application to answer client requests. Servlets are.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
Java Server Pages. JSP Documents JSP docs are XHTML Documents containing: –Fixed-Template Data: FTD HTML Components XML markup –JSP Components:
Objectives Java Servlet Web Components
JSP Java Server Pages Softsmith Infotech.
CSC 2720 Building Web Applications
Java Servlets and Java Server Pages Carol Wolf Computer Science.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
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.
JSP Presented by K.Venkata Ratnam HOD MCA (Dept) Newton’s Institute of Engineering.
1 CS122B: Projects in Databases and Web Applications Spring 2015 Notes 03: Web-App Architectures Professor Chen Li Department of Computer Science CS122B.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
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.
Java Server Pages (JSP)
Java Servlet API CGI / HTTP Concepts Java Servlet API.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
Java Servlets and Java Server Pages Norman White Stern School of Business.
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
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.
Mark Dixon 1 11 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
Java and the Web CSE 3330 Southern Methodist University.
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
Introduction To HTML Dr. Magdi AMER. HTML elements.
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,
JSP JavaServer Pages. What is JSP? Java based technology that simplifies the development of dynamic web sites JSP pages are HTML pages with embedded code.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
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.
1 Web Programming with Servlets & JSPs WEB APPLICATIONS – AN OVERVIEW.
Speaker Name Speaker Title Speaker Title Oracle Corporation Olivier Le Diouris Principal Product Manager Oracle Corporation Building Servlet and JSP Applications.
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.
Java Server Pages.
CS122B: Projects in Databases and Web Applications Winter 2019
Presentation transcript:

Servlet and JSP Programming: An Introduction Spiros Papadimitriou

Overview  Introduction  Static vs. Dynamic  Servlets  Java Server Pages (JSP)

Introduction  What you know: –Java –JDBC  What we’ll tell you: –How to use Java inside a webserver!  Why you might care: –Building non-trivial web interfaces and applications –In particular: database-backed web applications

Plain Old Static Pages  Static webpages –Plain files stored in the filesystem –Webserver accepts pathnames –Returns contents of corresponding file  Boring! –Can’t generate customized content—always serve the same static pages…

Static Pages GET /path/index.html Web Server Filesystem read /path/index.html STATUS …

Dynamic Content  CGI: Easy “fix” –Common Gateway Interface –Oldest standard –But at least a standard! –Inefficient –No persistent state  Forward requests to external programs –Spawn one process for each new request (ouch!) –Communicate via Standard input/output Environment variables –Process terminates after request is handled

CGI GET /cgi-bin/query?KEY=foo STATUS … CGI Program KEY=foo <response text foo> Web Server Filesystem Database, etc stdin stdout

CGI GET /cgi-bin/query?KEY=bar STATUS … CGI Program KEY=bar <response text bar> Web Server Filesystem Database, etc stdin stdout

Servlets to the rescue…  Little Java programs… –Contain application-specific code –Web server does generic part of request handling –Servlets run “in” the web server and do some of the handling  Highlights –Standard! –Efficiency (much better than CGI) –Security (Java!) –Persistence (handle multiple requests)

Servlets GET /srv/pkg.Servlet?KEY=foo STATUS … Web Server Filesystem Database, etc JVM Servlet doGet(req, res) res

Servlets GET /srv/pkg.Servlet?KEY=foo STATUS … Web Server Filesystem Database, etc JVM Servlet doGet(req, res) res

Servlet Example package pkg; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Servlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println(" Sample Servlet"); out.println(" "); out.println(" Hello World at " + req.getRequestURI() + " ! "); out.println(” Key is " + req.getParameter("KEY")); out.println(” "); }

More Servlets  A catalog of servlet methods: –init(config) –service(req, res) doGet(req, res) doPut(req, res) doDelete(req, res) etc…  A catalog of request methods: –getParameter(name) –getParameterNames(), getParameterValues(name) –getCookies()  A catalog of response methods: –sendRedirect(url), sendError(errCode, errStr) –setContentType(mimeType) –addCookie(cookieObj)

JSP… Sugar  Printing to output not really a special case when writing heaps of HTML!  Well… why not make that the common case in the syntax and program statements the “special case?”  And while we’re at it, why not hide the compilation steps (let the server do it)?  Now things look more like “plain old HTML” (only they aren’t… they can produce dynamic content and really are Java programs)

First JSP Example (1/3) Sample JSP Hello World! Key is  Core syntactic elements: –Directives –Declarations (outside service method) –Scriptlets –Expressions

First JSP Example (2/3) package jsp._jsp; // line:/jsp/hello.jsp:1 import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public class _hello_2ejsp extends org.gjt.jsp.HttpJspPageImpl implements org.gjt.jsp.GnuJspPage { […] public void _jspService (HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { response.setContentType ("text/html"); […] HttpSession session = pageContext.getSession (); JspWriter out = pageContext.getOut (); […]

First JSP Example (3/3) try { // line:/jsp/hello.jsp:2 out.print(" \n[…]"); // line:/jsp/hello.jsp:4 String s = "GNUJSP"; // line:/jsp/hello.jsp:5 out.print("\n […]"); // line:/jsp/hello.jsp:6 out.print(s); […] // line:/jsp/hello.jsp:7 out.print(“Scriptlet Statement Hello!”); […] // line:/jsp/hello.jsp:8 out.print(request.getParameter(“KEY”)); // line:/jsp/hello.jsp:8 out.print(“ \n […]”); } catch (Throwable e) { […] } }

Second JSP Example (1/2) <%! Static final String dbHost = “…”; %> Static final String oracleDriver = “…”; %> <% String courseId = request.getParameter(“courseid”); Class.forName(oracleDriver); Connection conn = DriverManager.getConnection(…); Statement stmt = con.createStatement(); String qry = “SELECT STUDENT.SID, SNAME, COURSE.CID ” + “FROM STUDENT, COURSE, TAKE ” + “WHERE TAKE.CID = ‘” + courseId + “’ AND ” + “STUDENT.SID = TAKE.SID AND ” + “COURSE.CID = TAKE.CID;” ResultSet res = conn.executeQuery(qry); %>

Second JSP Example (2/2) Student Information Student Information for CourseID Student StudentID

Alternatives  Dynamically loadable server modules –Web server offers standard API for request handlers –Request handlers are dynamic libraries –Loaded into server address space (security?!) –Very efficient! –Better suited for generic server extensions than application-specific logic  CGI flavors –Persistent CGI (PCGI) / FastCGI –Too little, too late  Modern web publishing systems –Essentially databases that “speak HTTP”

More Information  Servlets and JSPs: – –  JDBC: –