COMP9321 Web Application Engineering

Similar documents
COMP9321 Web Application Engineering

COMP9321 Web Application Engineering

JavaServer Pages (JSP)

JavaServer Pages. What is JavaServer Pages?

Fast Track to Java EE 5 with Servlets, JSP & JDBC

Introduction to JSP and Servlets Training 5-days

CSc31800: Internet Programming, CS-CCNY, Spring 2004 Jinzhong Niu May 9, JSPs 1

Java Server Page (JSP)

Servlets1. What are Servlets? Where are they? Their job. Servlet container. Only Http?

CE212 Web Application Programming Part 3

112. Introduction to JSP

Java Enterprise Edition. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 1

Fast Track to Java EE

JSP. Basic Elements. For a Tutorial, see:

112-WL. Introduction to JSP with WebLogic

COMP9321 Web Application Engineering

COMP9321 Web Application Engineering

COMP9321 Web Application Engineering

a. Jdbc:ids://localhost:12/conn?dsn=dbsysdsn 21. What is the Type IV Driver URL? a. 22.

Université du Québec à Montréal

Advance Java. Configuring and Getting Servlet Init Parameters per servlet

This course is intended for Java programmers who wish to write programs using many of the advanced Java features.

JSP - SYNTAX. Any text, HTML tags, or JSP elements you write must be outside the scriptlet. Following is the simple and first example for JSP:

Principles and Techniques of DBMS 6 JSP & Servlet

JSP (Java Server Page)

Java Server Pages. JSP Part II

Database Systems Lab. 11. JSP I 충남대학교컴퓨터공학과 데이타베이스시스템연구실

Contents at a Glance

A Gentle Introduction to Java Server Pages

Introduction to Java Server Pages. Enabling Technologies - Plug-ins Scripted Pages

PSD1B Advance Java Programming Unit : I-V. PSD1B- Advance Java Programming

JSP Scripting Elements

Unit 5 JSP (Java Server Pages)

CSC309: Introduction to Web Programming. Lecture 10

ADVANCED JAVA COURSE CURRICULUM

CSC309: Introduction to Web Programming. Lecture 11

Web applications and JSP. Carl Nettelblad

Web Programming. Lecture 11. University of Toronto

Basic Principles of JSPs

A.1 JSP A.2 JSP JSP JSP. MyDate.jsp page contenttype="text/html; charset=windows-31j" import="java.util.calendar" %>

Writing Servlets and JSPs p. 1 Writing a Servlet p. 1 Writing a JSP p. 7 Compiling a Servlet p. 10 Packaging Servlets and JSPs p.

COMP9321 Web Application Engineering

Introduction. This course Software Architecture with Java will discuss the following topics:

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering ECOM Advanced Internet Technology Lab.

Servlet and JSP Review

Introduction. Literature: Steelman & Murach, Murach s Java Servlets and JSP. Mike Murach & Associates Inc, 2003

Advanced Java Programming

JAVA 2 ENTERPRISE EDITION (J2EE)

Servlets. An extension of a web server runs inside a servlet container

Component Based Software Engineering

directive attribute1= value1 attribute2= value2... attributen= valuen %>

JSP MOCK TEST JSP MOCK TEST IV

ADVANCED JAVA TRAINING IN BANGALORE

Table of Contents. Introduction... xxi

01KPS BF Progettazione di applicazioni web

JAVA SERVLET. Server-side Programming INTRODUCTION

AN ISO 9001:2008 CERTIFIED COMPANY ADVANCED. Java TRAINING.

JSP source code runs on the web server via JSP Servlet Engine. JSP files are HTML files with special Tags

Java E-Commerce Martin Cooke,

Servlets and JSP (Java Server Pages)

2. Follow the installation directions and install the server on ccc. 3. We will call the root of your installation as $TOMCAT_DIR

LearningPatterns, Inc. Courseware Student Guide

Trabalhando com JavaServer Pages (JSP)

COMP201 Java Programming

Java Server Pages JSP

Peers Techno log ies Pv t. L td. Core Java & Core Java &Adv Adv Java Java

JSP CSCI 201 Principles of Software Development

DVS WEB INFOTECH DEVELOPMENT TRAINING RESEARCH CENTER

Enterprise Java Technologies (Part 1 of 3) Component Architecture. Overview of Java EE. Java Servlets

Servlet Basics. Agenda

CS506 Web Design & Development Final Term Solved MCQs with Reference

About the Authors. Who Should Read This Book. How This Book Is Organized

Module 5 Developing with JavaServer Pages Technology

Scope and State Handling in JSP

Call us: /

PES INSTITUTE OF TECHNOLOGY, SOUTH CAMPUS DEPARTMENT OF MCA INTERNAL TEST (SCHEME AND SOLUTION) II

Chapter 2 How to structure a web application with the MVC pattern

Java- EE Web Application Development with Enterprise JavaBeans and Web Services

Module 3 Web Component

Integrating Servlets and JavaServer Pages Lecture 13

INTRODUCTION TO SERVLETS AND WEB CONTAINERS. Actions in Accord with All the Laws of Nature

A JavaBean is a class file that stores Java code for a JSP

GUJARAT TECHNOLOGICAL UNIVERSITY

J2EE Development. Course Detail: Audience. Duration. Course Abstract. Course Objectives. Course Topics. Class Format.

Java Server Pages, JSP

Trabalhando com JavaServer Pages (JSP)

Servlet Fudamentals. Celsina Bignoli

SECTION I: JAVA SERVLETS AND JAVA SERVER PAGES

1. Introduction. 2. Life Cycle Why JSP is preferred over Servlets? 2.1. Translation. Java Server Pages (JSP) THETOPPERSWAY.

SSC - Web applications and development Introduction and Java Servlet (I)

Oracle 10g: Build J2EE Applications

Advanced Web Technology

Ch04 JavaServer Pages (JSP)

JSP - ACTIONS. There is only one syntax for the Action element, as it conforms to the XML standard:

How to structure a web application with the MVC pattern

LTBP INDUSTRIAL TRAINING INSTITUTE

Unit 4 Java Server Pages

1.264 Lecture 15. Web development environments: JavaScript Java applets, servlets Java (J2EE) Active Server Pages

Model View Controller (MVC)

Stateless -Session Bean

Transcription:

COMP9321 Web Application Engineering Java Server Pages (JSP) Dr. Basem Suleiman Service Oriented Computing Group, CSE, UNSW Australia Semester 1, 2016, Week 3 http://webapps.cse.unsw.edu.au/webcms2/course/index.php?cid=2442 1

Acknowledgement/Contributions Service Oriented Computing Group, CSE, UNSW Australia Dr. Helen Paik Prof. Boualem Bentallah Dr. Srikumar Venugopal Dr. Moshe Chai Barukh Dr. Amin Beheshti Dr. Basem Suleiman Many others from service oriented computing group 2

Review: Java Servlets The Request: <html> <head><title>e-mail Form</title></head> <body> <h3>enter your name and e-mail address. <br />Then click the Send button to send the data to the server.</h3> <form method = "get" action="http://localhost:8080/servlet/echo.emailservlet"> <p><input type = "text" name = "name" value = "" size = 30 /> Name </p> <p><input type = "text" name = "email" value = "" size = 30 /> E-Mail Address </p> <p><input type= "submit" value="send" /></p> </form> </body> </html> EmailServlet processes a request from a web page. It responds to the request by echoing back the name and email address that was sent in. 3

The Servlet: Review: Java Servlets package echo; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class EmailServlet extends HttpServlet { protected void doget (HttpServletRequest request, HttpServletResponse response) { try{ response.setcontenttype ("text/html"); PrintWriter out = response.getwriter (); String name = request.getparameter ("name"); String email = request.getparameter ("email"); } out.println ("<html><body>"); out.println ("<h3>hello.</h3>"); out.println ("<h3>" + name+ "</h3>"); out.println ("<h3>your email address is " + email + "</h3>"); out.println ("</body></html>"); }catch (IOException e) {System.out.println ("Servlet Exception");} 4

Review: Java Servlets Deployment Descriptor : web.xml ------------------------------------------------------------------- <?xml version="1.0" encoding="iso-8859-1"?> <web-app> <servlet> <servlet-name>emailservlet</servlet-name> <servlet-class>echo.emailservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>emailservlet</servlet-name> <url-pattern>/servlet/echo.emailservlet</url-pattern> </servlet-mapping> </web-app> 5

JavaServer Pages (JSP) Technology JavaServer Pages (JSP) is a server-side development technology that enables the creation of web content (both static and dynamic components) JSP makes available all the dynamic capabilities of Java Servlet technology; but provides a more natural approach to creating static content JSP is similar to PHP, but it uses the Java programming language To deploy and run JavaServer Pages, a compatible web server with a servlet container, such as Apache Tomcat, is required 6

Main Features of JSP technology A language for developing JSP pages; text-based documents that describe how to process a request and construct a response; An Expression Language (EL) for accessing server-side objects; Mechanisms for defining extensions to the JSP language; Platform independent, and an integral part of Java EE JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB It can be used in combination with servlets to handle the business logic Performance is significantly better because JSP allows embedding Dynamic Elements in HTML Pages itself instead of having a separate CGI files 7

JSP Page A JSP page is a text document that consists of: Static data: o which can be expressed in any text-based format (such as HTML, SVG, WML, and XML); JSP elements: o which construct dynamic content; o The recommended file extension for the source file of a JSP page is.jsp. o The recommended extension for the source file of a fragment of a JSP page is.jspf. 8

JSP Directives General format <%@ directive attribute="value" %> <%= %> is used for expressions e.g. <%= request.getparameter ("email") %> <%! %> is used for declarations. e.g. <%! String name, email; %> <% %> is used for straight Java code. e.g. <% if (x > 5) { %> <%@ %> is used to include another file such as an HTML file or a package such as java.sql.*. e.g. <%@ page contenttype="text/html; charset=utf-8" %> e.g. <%@ taglib uri="http://java.sun.com/jsp/jstl/core " prefix="c" %> 9

JSP Example The Request: <html> <body> <h3>enter your name and email address: </h3> <form method="get" action="hello.jsp"> <p><input type="text" name="name" value="" size="20"/> Name </p> <p><input type="text" name="email" value="" size="20"/> Email </p> <p><input type="submit" name="send" value="send"/> </p> </form> </body> </html> 10

JSP Example JSP File: <%@ page contenttype="text/html; charset=utf-8" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core " %> <html> <body> <%! String name, email; %> <jsp:usebean id="hello" scope="session" class="greetings.hellobean" /> <jsp:setproperty name="hello" property="name" value='<%= request.getparameter ("name") %> /> <jsp:setproperty name="hello" property="email" value= <%= request.getparameter ("email") %> /> <% name = hello.getname(); email = hello.getemail(); out.println ("<h3>hello, your name is " + name); out.println (" and your email address is " + email + ".</h3>"); %> </body></html> 11

JSP Example JSP File: <%@ page contenttype="text/html; charset=utf-8" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core " %> <html> <body> <%! String name, email; %> <%@page... %> <jsp:usebean id="hello" scope="session" class="greetings.hellobean" /> <jsp:setproperty page directive. name="hello" property="name" value='<%= request.getparameter ("name") %> /> <jsp:setproperty sets the name="hello" content type property="email" returned by the page. value= <%= request.getparameter ("email") %> /> <% name = hello.getname(); email = hello.getemail(); out.println ("<h3>hello, your name is " + name); out.println (" and your email address is " + email + ".</h3>"); %> </body></html> 12

JSP Example JSP File: <%@ page contenttype="text/html; charset=utf-8" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core " %> <html> <body> <%! String name, email; %> <%@taglib... %> <jsp:usebean id="hello" scope="session" class="greetings.hellobean" /> <jsp:setproperty Tag library name="hello" directives. property="name" value='<%= import request.getparameter custom tag libraries. ("name") %> /> <jsp:setproperty name="hello" property="email" JavaServer value= <%= Pages request.getparameter Standard Tag Library ("email") (JSTL): %> /> <% JSTL extends the JSP specification by adding a tag library name = hello.getname(); of JSP tags for common tasks, such as conditional %> </body></html> email = hello.getemail(); execution, loops, and database access. out.println ("<h3>hello, your name is " + name); out.println (" and your email address is " + email + ".</h3>"); 13

JSP Example JSP File: <%@ page contenttype="text/html; charset=utf-8" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core " %> <html> <body> <%! String name, email; %> <jsp:usebean id="hello" scope="session" class="greetings.hellobean" /> <jsp:setproperty name="hello" property="name" value='<%= request.getparameter <jsp:usebean ("name") >%> /> <jsp:setproperty name="hello" property="email" locales value= <%= and initializes request.getparameter an identifier that ("email") points %> to that /> object. <% name is used = hello.getname(); to locate or instantiate a bean class. email = hello.getemail(); out.println ("<h3>hello, your name is " + name); object (the bean). out.println (" and your email address is " + email + ".</h3>"); %> Each JavaServer page can be associated with a Java bean. </body></html> is a standard element that creates an object containing a collection of JavaBeans are classes that encapsulate many objects into a single 14

JSP Example JSP File: <%@ page contenttype="text/html; charset=utf-8" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core " %> <html> <body> <%! String name, email; %> <jsp:usebean id="hello" scope="session" class="greetings.hellobean" /> <jsp:setproperty name="hello" property="name" value='<%= request.getparameter ("name") %> /> <jsp:setproperty name="hello" <jsp:setproperty property="email" > value= <%= request.getparameter ("email") %> /> is a standard element that sets the value of an object property. <% name = hello.getname(); email = hello.getemail(); out.println ("<h3>hello, your name is " + name); out.println (" and your email address is " + email + ".</h3>"); %> </body></html> 15

JSP Example JSP File: <%@ Some page reserved contenttype="text/html; words (JSP charset=utf-8" Objects): %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core " %> <html> request an instance of HttpServletRequest. <body> response an instance of HttpServletResponse. <%! String name, email; %> out a PrintWriter object for the response. <jsp:usebean id="hello" scope="session" class="greetings.hellobean" /> <jsp:setproperty name="hello" property="name" application value='<%= request.getparameter an instance of ServletContext ("name") %> /> <jsp:setproperty name="hello" property="email" value= <%= request.getparameter ("email") %> /> <% name = hello.getname(); email = hello.getemail(); out.println ("<h3>hello, your name is " + name); out.println (" and your email address is " + email + ".</h3>"); %> </body></html> session the HttpSession object associated with the session. 16

JSP Example The Bean: public class HelloBean { private String name = ""; private String email = ""; public String getname() {return name;} public String getemail() {return email;} public void setname (String n) {name = n;} public void setemail (String e) {email = e;} } // HelloBean Each Java server page is associated with a Java bean. These are Java programs and reside on the server. o The constructor has no parameters o All variables have accessor (get) and mutator (set) methods. 17

JSP Lifecycle 18

Processing JSP Files 19

JSP Parsing and Compilation 20

Let us Revisit the WelcomeServlet 21

Here is equivalent in JSP (welcome.jsp) 22

JSP Basics Scripting Elements Traditional Modern Scriptlet Expression Declaration Comments EL Scripting ${ } JSP Page JSP Elements Directive Elements Page Include Taglib Action Elements Template Text (HTML bits ) custom Standard <abc:mytag> <jsp:usebean> <jsp:getproperty> <jsp:setproperty> <jsp:include> <jsp:forward> <jsp:param> 23

JSP Basics Scripting Elements Traditional Modern Scriptlet Expression Declaration Comments EL Scripting ${ } JSP Page JSP Elements Directive Elements Page Include Taglib Action Elements Template Text (HTML bits ) custom Standard <abc:mytag> <jsp:usebean> <jsp:getproperty> <jsp:setproperty> <jsp:include> <jsp:forward> <jsp:param> 24

JSP Elements: JSP directives 25

JSP Basics Scripting Elements Traditional Modern Scriptlet Expression Declaration Comments EL Scripting ${ } JSP Page JSP Elements Directive Elements Page Include Taglib Action Elements Template Text (HTML bits ) custom Standard <abc:mytag> <jsp:usebean> <jsp:getproperty> <jsp:setproperty> <jsp:include> <jsp:forward> <jsp:param> 26

JSP Elements: JSP Scripting (expression) 27

JSP Elements: Using the implicit objects request: the HttpServletRequest object response: the HttpServletResponse object session: the HttpSession object associated with the request out: the Writer object config: the ServletCong object application: the ServletContext object Example: <html><body> <h2>jsp expressions</h2> <ul> <li>current time is: <%= new java.util.date() %> <li>server Info: <%= application.getserverinfo() %> <li>servlet Init Info: <%= config.getinitparameter("webmaster") %> <li>this Session ID: <%= session.getid() %> <li>the value of <code>testparam</code> is: <%= request.getparameter("testparam") %> </ul> </body></html> 28

JSP Elements: JSP Scripting (scriptlet) JSP scriptlet, are inserted verbatim into the translated servlet code. The scriptlet can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language. Within a scriptlet, you can do any of the following: Declare variables or methods to use later in the JSP page. Write expressions valid in the page scripting language. Use any of the implicit objects or any object declared with a <jsp:usebean> element. Write any other statement valid in the scripting language used in the JSP page. Remember that JSP expressions contain `(string) values', but JSP scriptlets contain `Java statements'. 29

Example: JSP Elements: JSP Scripting (scriptlet) <HTML> <BODY> <% // This scriptlet declares and initializes "date" java.util.date date = new java.util.date(); %> Hello! The time is: <% out.println( date ); out.println( "<BR>Your machine's address is: " ); out.println( request.getremotehost()); %> </BODY> </HTML> 30

JSP Elements: JSP Scripting (scriptlet) The following three examples, generate the same output 31

JSP Elements: JSP Scripting (scriptlet) Example, setting the background of a page (CoreServlet p.334) 32

JSP Elements: JSP Scripting (scriptlet) You can also use the scriptlet to conditionally generate HTML. 33

JSP Elements: JSP Scripting (comment) 34

Attributes in a JSP Recall from last week. Request attributes and RequestDispatcher: We use request attributes when we want some other component of the application take over all or part of your request. (HeadFirst) p.309 35

JSP Basics Scripting Elements Traditional Modern Scriptlet Expression Declaration Comments EL Scripting ${ } JSP Page JSP Elements Directive Elements Page Include Taglib Action Elements Template Text (HTML bits ) custom Standard <abc:mytag> <jsp:usebean> <jsp:getproperty> <jsp:setproperty> <jsp:include> <jsp:forward> <jsp:param> 36

JSP Elements: JSP Actions (HeadFirst) p.309 37

JSP Elements: JSP Actions (include) 38

jsp:include vs. include directive (CoreServlet p.380) 39

JSP Elements: JSP Actions (forward) 40

JSP Elements: JSP Actions (usebean) 41

JSP Elements: JSP Actions (usebean) 42

JSP Elements: JSP Actions (usebean) 43

JSP Elements: JSP Actions (usebean) Sharing Beans: using scope attribute 44

JSP Elements: JSP Actions (usebean) Sharing Beans: using scope attribute 45

JSP Basics Scripting Elements Traditional Modern Scriptlet Expression Declaration Comments EL Scripting ${ } JSP Page JSP Elements Directive Elements Page Include Taglib Action Elements Template Text (HTML bits ) custom Standard <abc:mytag> <jsp:usebean> <jsp:getproperty> <jsp:setproperty> <jsp:include> <jsp:forward> <jsp:param> 46

Expression Language (EL) in JSP 47

Expression Language (EL) in JSP 48

Expression Language (EL) in JSP Towards Script-less JSP 49

Expression Language (EL) in JSP (HeadFIrst) p.367 50

Expression Language (EL) in JSP 51

EL Basics: Accessing Scoped Variables 52

EL Basics: Accessing Scoped Variables 53

EL Basics: Using dot vs. Using [ ] operator 54

EL Basics: Using dot vs. Using [ ] operator 55

EL Basics: Using dot vs. Using [ ] operator 56

EL Basics: Using dot vs. Using [ ] operator 57

EL Basics: Using dot vs. Using [ ] operator 58

EL Basics: Using dot vs. Using [ ] operator 59

EL Basics: EL Implicit Objects 60

EL Basics: EL Implicit Objects 61

EL Basics: EL Implicit Objects 62

EL Basics: EL Operators 63

JSP Standard Tag Library (JSTL) 64

JSP Standard Tag Library (JSTL) 65

JSP Standard Tag Library (JSTL) 66

JSP Standard Tag Library (JSTL) 67

JSP Standard Tag Library (JSTL) 68

JSTL Basics: Looping collections 69

JSTL Basics: Looping collections 70

JSTL Basics: Looping collections 71

JSTL Basics: Conditional output 72

JSTL Basics: Conditional output 2- https://www.ibm.com/developerworks/library/j-jstl0318/ 73

JSTL Basics: Using <c:set> 74

JSTL Basics: Using <c:set> 75

JSTL Basics: Working with URL 76

Other things available in JSTL 77

JSP Basics Scripting Elements Traditional Modern Scriptlet Expression Declaration Comments EL Scripting ${ } JSP Page JSP Elements Directive Elements Page Include Taglib Action Elements Template Text (HTML bits ) custom Standard <abc:mytag> <jsp:usebean> <jsp:getproperty> <jsp:setproperty> <jsp:include> <jsp:forward> <jsp:param> 78

JSP Custom Tags 79

JSP Custom Tags 80

JSP Custom Tags 81

JSP Custom Tags 82

JSP Custom Tags 83

JSP Custom Tags 84

JSP 85

Servlet Exercise Lab Exercises (Week 4) Environment and project Configurations Deployment of Servlet examples (week 2 and 3) Implement a servlet that displays current time Journey Exercise The Servlet Version The Simple JSP Version JSP with EL + JSTL Extensions (Journey destinations: LocationBean) 86

Message Board 87