CS 112 Introduction to Programming Web Programming: Backend (server side) Programming with Servlet, JSP Yang (Richard) Yang Computer Science Department.

Slides:



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

CS4273: Distributed System Technologies and Programming I Lecture 11: JavaServer Pages (JSP)
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.
Languages for Dynamic Web Documents
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
An introduction to Java Servlet Programming
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
Introduction to Servlet & JSP
1 Java Server Pages Can web pages be created specially for each user? What part does Java play?
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems JavaServer Pages (JSP)
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
CSCI 6962: Server-side Design and Programming History and Background.
1 CS428 Web Engineering Lecture 18 Introduction (PHP - I)
Java Servlets and JSP.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
JSP Java Server Pages Softsmith Infotech.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
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.
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
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.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
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.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
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)
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.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
Introduction to Servlets Allen Day. Notes This is a training NOT a presentation Please ask questions Prerequisites.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Core basic Java web server technologies. Tools Eclipse IDE for Java EE Developers (Netbeans also works) nloads/packages/eclipse-
©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.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Introduction To HTML Dr. Magdi AMER. HTML elements.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
 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 Programming: Advanced Topics 1 Building Web Applications Chapter 13.
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.
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.
Google App Engine using Java 1. Outline Getting started Guestbook example Todo example Simplified Madlib 2.
CS 112 Introduction to Programming GUI Programming; Web as UI; Class Wrap Yang (Richard) Yang Computer Science Department Yale University 208A Watson,
World Wide Web has been created to share the text document across the world. In static web pages the requesting user has no ability to interact with the.
1 Web Programming with Servlets & JSPs WEB APPLICATIONS – AN OVERVIEW.
Google App Engine using Java 1. Outline Getting started Guestbook example Todo example Simplified Madlib 2.
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
CS122B: Projects in Databases and Web Applications Winter 2017
Java Servlets By: Tejashri Udavant..
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Knowledge Byte In this section, you will learn about:
CS122B: Projects in Databases and Web Applications Winter 2018
CS122B: Projects in Databases and Web Applications Spring 2018
Java Servlets and JSP.
Presentation transcript:

CS 112 Introduction to Programming Web Programming: Backend (server side) Programming with Servlet, JSP Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:

2 Outline  Admin and recap  Server-side web programming overview  Servlet programming  Java servlet page (JSP)

3 Admin  PS8 Part 2 (Bulldog tournament) m Due Tuesday at 11:55 pm  Project checkpoint m Due Thursday at 11:55 pm (Please use the CHECKPOINT template)  Project demo day m 11:30 – 12:30 pm Apr. 30 at TEAL

Recap: HTML/CSS/Javascript  HTML: page content (model) m Document object model (DOM)  CSS: display of content (view)  Javascript: interaction

5 Outline  Admin and recap  Server-side web programming overview

6 Web App--Server Side: Motivating Example (OPS2b) Madlib (template): One of the most characters in fiction is named "Tarzan of the." Tarzan was raised by a/an and lives in the jungle in the heart of darkest. He spends most of his time eating and swinging from tree to. Whenever he gets angry, he beats on his chest and says, " !" This is his war cry. Tarzan always dresses in shorts made from the skin of a/an and his best friend is a/an chimpanzee named Cheetah. He is supposed to be able to speak to elephants and. In the movies, Tarzan is played by.

7 Web App--Server Side: Motivating Example Welcome to the game of Mad Libs. I will ask you to provide several words and phrases to fill in a mad lib story. The result will be displayed to you. Template file? tarzan.txt Please input an adjective: silly Please input a plural noun: apples Please input a noun: frisbee Please input an adjective: hungry Please input a place: New Haven, CT Please input a plural noun: bees Please input a noun: umbrella Please input a funny noise: burp Please input an adjective: shiny Please input a noun: jelly donut Please input an adjective: beautiful Please input a plural noun: spoons Please input a person's name: Keanu Reeves Commandline Madlib (interaction): Your mad-lib has been created: One of the most silly characters in fiction is named "Tarzan of the apples." Tarzan was raised by a/an frisbee and lives in the hungry jungle in the heart of darkest New Haven, CT. He spends most of his time eating bees and swinging from tree to umbrella. Whenever he gets angry, he beats on his chest and says, " burp !" This is his war cry. Tarzan always dresses in shiny shorts made from the skin of a/an jelly donut and his best friend is a/an beautiful chimpanzee named Cheetah. He is supposed to be able to speak to elephants and spoons. In the movies, Tarzan is played by Keanu Reeves. pseudo code?

Cmdline Madlib Pseudo Code Display welcome message Ask user to choose story template Process given template file foreach token in template file ask user input store token -> user input mapping Display story for each line for each word in line if word is token print user input else print word as it is Web presents a different medium to deliver app. The unit of interaction is Web page. Q: what Web pages?

Webpage-Based Programming  One way to think about a dynamic Web page is to think it as an object: it is constructed with given input parameters  A Web page may collect input from user and then create another Web page (object)--browsing P1 input P2 input P3 input

Input to Create Each Page? Display welcome message Ask user to choose story template file Process given template file foreach token in template file ask user input store token -> user input mapping Display story for each line for each word in line if word is token print user input else print word as it is Input: None Input Chosen template Who collect? Input User input for each token Chosen Template Who collect? P1 P2 P3

Server-side Page Creation input parameter created html page Q: What is the mechanism in Web to collect user input?

HTML Form  Form

HTML Form  A form gives instruction to the browser on m inputs to be collected from user m URL (program) to be invoked when sending to server Step 1: Please pick an input story template: Program to be invoked An input item to be collected How input is transported to server: get/post Invisible on page. Used to pass internal data

Form Input Type   Thin Thick Cheese

Web Execution Flow: Client Side  After user clicks submit on a form, browser m encodes each input in a format = m requests page with URL: ? = & =

Web Execution Flow: Server Side  Web server receives the request for the URL, finds the application for the URL, and passes the values of the parameters to the application  The application retrieves the parameters, generates a Web page, and sends the page back to the browser to display

Google App Engine 17  A platform to simplify the development and hosting of Web applications m Simplified deployment Load balancing Automatic scaling m Useful high-level services and APIs

Discussion: What GAE can do to allow simple, flexible server-side app programming?  Client browser m After user clicks submit on a form, browser encodes each input in a format = requests page with URL: ? = & =  Web server m receives the request for the URL, m finds the application for the URL, and m passes the values of the parameters to the application. m the application retrieves the request parameters, generates a response Web page back to the browser to display web.xml: User defines URL mapping

Discussion: What GAE can do to allow simple, flexible server-side app programming?  Client browser m After user clicks submit on a form, browser encodes each input in a format = requests page with URL: ? = & =  Web server m receives the request for the URL, m finds the application for the URL, and m passes the values of the parameters to the application. m the application retrieves the request parameters, generates a response Web page back to the browser to display Define Servlet class: conduct common processing steps; app overrides key step ( doGet/doPost ) to define app-specific behavior

Discussion: What GAE can do to allow simple, flexible server-side app programming?  Client browser m After user clicks submit on a form, browser encodes each input in a format = requests page with URL: ? = & =  Web server m receives the request for the URL, m finds the application for the URL, and m passes the values of the parameters to the application. m the application retrieves the request parameters, generates a response Web page back to the browser to display Define HttpServletRequest class to allow simple methods to retrieve parameter, e.g., getParameter(“name”)

Discussion: What GAE can do to allow simple, flexible server-side app programming?  Client browser m After user clicks submit on a form, browser encodes each input in a format = requests page with URL: ? = & =  Web server m receives the request for the URL, m finds the application for the URL, and m passes the values of the parameters to the application. m the application retrieves the request parameters, generates a response Web page back to the browser to display Define HttpServletResponse class to allow simple methods to write response (not worry about sending across network), e.g., getWriter().println( “ Hello”)

Example Display welcome message Ask user to choose story template file Process given template file foreach token in template file ask user input store token -> user input mapping Display story for each line for each word in line if word is token print user input else print word as it is Input: None index.html (see Note)

web.xml 23 <web-app xmlns:xsi=" xmlns=" xmlns:web=" xsi:schemaLocation=" version="2.5”> index.html

Example Display welcome message Ask user to choose story template file Process given template file foreach token in template file ask user input store token -> user input mapping Display story for each line for each word in line if word is token print user input else print word as it is Input: Chosen template CreateMadlibServlet.java

web.xml  Allow a WebApp developer to declare the applications (servlets), and which servlet will serve which URL 25 <web-app xmlns:xsi=" xmlns=" xmlns:web=" xsi:schemaLocation=" version="2.5"> CreateMadlibServlet madlibs.CreateMadlibServlet CreateMadlibServlet /create index.html

Example Display welcome message Ask user to choose story template file Process given template file foreach token in template file ask user input store token -> user input mapping Display story for each line for each word in line if word is token print user input else print word as it is Input: User input for each token/template DisplayMadlibServlet.java

web.xml  Allow a WebApp developer to declare the applications (servlets), and which servlet will serve which URL 27 <web-app xmlns:xsi=" xmlns=" xmlns:web=" xsi:schemaLocation=" version="2.5"> CreateMadlibServlet madlibs.CreateMadlibServlet DisplayMadlibServlet madlibs.DisplayMadlibServlet CreateMadlibServlet /create DisplayMadlibServlet /display index.html

28 Outline  Admin and recap  Server-side programming overview  Java Servlet  Java Servlet Page (jsp)

Motivation  Most used statement in CreateMadlibServlet.java? 29

Motivation  For many Web pages, most of the content will be static, and hence only a small portion will be generated dynamically  The Servlet architecture asks you to print the whole document (i.e., a large number of Java print and escape statements for printing the static part)  JSP goal: remove most of the print statements 30

Motivating Example  Generate an html page consisting of a list of 10 random numbers 31

Manual HTML 32 My list 1: 9 2: 3 3: 5 4: 1 5: 7 6: 4 7: 0 8: 2 9: 8 10: 9

Servlet 33 import java.util.Random; public class NumListServlet extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { doGet(req, resp); } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/html"); resp.getWriter().println(" "); resp.getWriter().println(” My list "); resp.getWriter().println(" "); Random rand = new Random(); for (int i = 1; i < 11; i ++) { resp.getWriter().print(” "); resp.getWriter().print( i + ”: ” + rand.nextInt(10) ); resp.getWriter().println(" "); } }

JSP Motivation 34 import java.util.Random; public class NumListServlet extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { doGet(req, resp); } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/html"); resp.getWriter().println(" "); resp.getWriter().println(” My list "); resp.getWriter().println(" "); Random rand = new Random(); for (int i = 1; i < 11; i ++) { resp.getWriter().print(” "); resp.getWriter().print(i + ”: ” + rand.nextInt(10) ); resp.getWriter().println(" "); } resp.getWriter().println(" "); }

JSP Motivation 35 My list <% Random rand = new Random(); for (int i = 1; i < 11; i ++) { resp.getWriter().print(” "); resp.getWriter().print(i + ”: ” + rand.nextInt(10) ); resp.getWriter().println(" "); } %> Directive Called a JSP scriptlet, containing a fragment of java code Why it does not work?

Run test.jsp as /test 36

web.xml 37 <web-app xmlns:xsi=" xmlns=" xmlns:web=" xsi:schemaLocation=" version="2.5"> CreateMadlibServlet madlibs.CreateMadlibServlet DisplayMadlibServlet madlibs.DisplayMadlibServlet TestJSP test.jsp TestJSP /test... index.html

JSP Motivation 38 My list <% Random rand = new Random(); for (int i = 1; i < 11; i ++) { response.getWriter().print(” "); response.getWriter().print(i + ”: ” + rand.nextInt(10) ); response.getWriter().println(" "); } %> Directive Called a JSP scriptlet, containing a fragment of java code implicit object See for implicit objectshttp://

Output 39

Problem 40 My list <% Random rand = new Random(); for (int i = 1; i < 11; i ++) { response.getWriter().print(” "); response.getWriter().print(i + ”: ” + rand.nextInt(10) ); response.getWriter().println(" "); } %> Use implicit object out to print. out != response.getWrite()

Fix 41 My list <% Random rand = new Random(); for (int i = 1; i < 11; i ++) { out.print(” "); out.print(i + ”: ” + rand.nextInt(10) ); out.println(" "); } %>

Motivation 42 My list <% Random rand = new Random(); for (int i = 1; i < 11; i ++) { out.print(” "); out.print(i + ”: ” + rand.nextInt(10) ); out.println(" "); } %> What if we do not want to use the print statement?

Attempt 43 My list <% Random rand = new Random(); for (int i = 1; i < 11; i ++) { %> i : rand.nextInt(10) <% } %> Problem: i and rand.NextInt(10) are not treated as Java exp.

JSP Expression 44 My list <% Random rand = new Random(); for (int i = 1; i < 11; i ++) { %> : <% } %> encloses JSP expression, whose value appears in the output to out.

Motivation: Reduce Embedded Scriptlets 45 My list <% Random rand = new Random(); for (int i = 1; i < 11; i ++) { %> : <% } %>

JSTL (JavaServer Pages Stanard Tag Library)  Objective: introduce JSP tags for common tasks such as conditional execution, loops, XML data access 46

Motivation: Reduce Embedded Scriptlets 47 My list <% Random rand = new Random(); %>

Java Servlet Page (JSP)  History m Released in 1999 m Similar to PHP but supports more sophisticated J2EE programming  Can be considered as a high-level abstraction of Java servlets  In real implementation, JSPs are translated to servlets at runtime 48

Exercise  Turn CreateMadlibServlet.java into createmadlib.jsp 49