COMP9321 Web Application Engineering

Similar documents
COMP9321 Web Application Engineering

COMP9321 Web Application Engineering

JavaServer Pages (JSP)

JavaServer Pages. What is JavaServer Pages?

COMP9321 Web Application Engineering

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

COMP9321 Web Application Engineering

Java Server Page (JSP)

JSP (Java Server Page)

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:

JSP Scripting Elements

112. Introduction to JSP

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

JSP. Basic Elements. For a Tutorial, see:

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

112-WL. Introduction to JSP with WebLogic

Introduction to JSP and Servlets Training 5-days

Principles and Techniques of DBMS 6 JSP & Servlet

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

Java Server Pages. JSP Part II

01KPS BF Progettazione di applicazioni web

ADVANCED JAVA COURSE CURRICULUM

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

COMP201 Java Programming

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

CE212 Web Application Programming Part 3

Université du Québec à Montréal

COMP9321 Web Application Engineering

JAVA 2 ENTERPRISE EDITION (J2EE)

JSP MOCK TEST JSP MOCK TEST IV

A Gentle Introduction to Java Server Pages

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

Java E-Commerce Martin Cooke,

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

Fast Track to Java EE

JSP CSCI 201 Principles of Software Development

CSC309: Introduction to Web Programming. Lecture 11

Web applications and JSP. Carl Nettelblad

Integrating Servlets and JavaServer Pages Lecture 13

Advance Java. Configuring and Getting Servlet Init Parameters per servlet

Servlet and JSP Review

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

Web Programming. Lecture 11. University of Toronto

Contents at a Glance

Unit 5 JSP (Java Server Pages)

Scope and State Handling in JSP

Trabalhando com JavaServer Pages (JSP)

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

DVS WEB INFOTECH DEVELOPMENT TRAINING RESEARCH CENTER

Modernizing Java Server Pages By Transformation. S h a n n o n X u T h o m a s D e a n Q u e e n s U n i v e r s i t y

ADVANCED JAVA TRAINING IN BANGALORE

CSC309: Introduction to Web Programming. Lecture 10

20/08/56. Java Technology, Faculty of Computer Engineering, KMITL 1

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

Trabalhando com JavaServer Pages (JSP)

/smlcodes /smlcodes /smlcodes JSP. Java Server Pages. Small Codes. Programming Simplified. A SmlCodes.Com Small presentation

More JSP. Advanced Topics in Java. Khalid Azim Mughal Version date: ATIJ More JSP 1/42

Advantage of JSP over Servlet

Java Server Pages, JSP

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

GUJARAT TECHNOLOGICAL UNIVERSITY

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

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

Session 21. Expression Languages. Reading. Java EE 7 Chapter 9 in the Tutorial. Session 21 Expression Languages 11/7/ Robert Kelly,

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

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

LTBP INDUSTRIAL TRAINING INSTITUTE

Session 11. Expression Language (EL) Reading

Call us: /

Basic Principles of JSPs

Sun Sun Certified Web Component Developer for J2EE 5 Version 4.0

Session 12. JSP Tag Library (JSTL) Reading & Reference

Java Server Pages. Copyright , Xiaoping Jia. 7-01/54

UNIT -5. Java Server Page

Advanced Java Programming

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.

Module 5 Developing with JavaServer Pages Technology

Java.. servlets and. murach's TRAINING & REFERENCE 2ND EDITION. Joel Murach Andrea Steelman. IlB MIKE MURACH & ASSOCIATES, INC.

Unit 4 Java Server Pages

Oracle Containers for J2EE

6- JSP pages. Juan M. Gimeno, Josep M. Ribó. January, 2008

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

Component Based Software Engineering

Java Server Pages JSP

Database. Request Class. jdbc. Servlet. Result Bean. Response JSP. JSP and Servlets. A Comprehensive Study. Mahesh P. Matha

Table of Contents. Introduction... xxi

Chapter 10 Servlets and Java Server Pages

LTBP INDUSTRIAL TRAINING INSTITUTE

Model View Controller (MVC)

Experiment No: Group B_2

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

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

UNIT 6:CH:14 INTEGRATING SERVLETS AND JSPTHE MVC ARCHITECTURE

Integrating Servlets and JSP: The MVC Architecture

Specialized - Mastering JEE 7 Web Application Development

JSP: Servlets Turned Inside Out

SECTION I: JAVA SERVLETS AND JAVA SERVER PAGES

Core Java. Basics of Java. String Handling. OOPS Concepts. Exception Handling

Ch04 JavaServer Pages (JSP)

JAVA. Web applications Servlets, JSP

Transcription:

COMP9321 Web Application Engineering Semester 2, 2017 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 http://webapps.cse.unsw.edu.au/webcms2/course/index.php?cid=2465 1

Review: Static vs. Dynamic Web Page A static web page is delivered to the user exactly as stored, in contrast to dynamic web pages which are generated by a web application, and on demand! is-a web page whose construction is controlled by an application server processing server-side scripts. is-a e.g. software framework that provides both facilities to create web applications and a server environment to run them. Java application servers http://docs.oracle.com/javaee/6/tutorial/doc/ It's core set of API and features are defined by Java EE. The Web modules include Java Servlets and JavaServer Pages (JSP). 2

Review: Java Servlets http://java.sun.com/products/servlet/index.jsp http://docs.oracle.com/javaee/6/tutorial/doc/bnafd.html 3

JavaServer Pages (JSP) Technology 4

JavaServer Pages (JSP) Technology JavaServer Pages (JSP) technology allows you to easily create web content that has both static and dynamic components. JSP technology makes available all the dynamic capabilities of Java Servlet technology; but provides a more natural approach to creating static content. JSPissimilartoPHP,butitusestheJavaprogramming language. To deploy and run JavaServer Pages, a compatible web server with a servlet container, such as Apache Tomcat, is required. 5

Main Features of JSP technology A language for developing JSP pages, which are 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; 6

JSP Page A JSP page is a text document that contains two types of text: Static data: o which can be expressed in any text-based format (such as HTML, SVG, WML, and XML); JSP elements: o o o which construct dynamic content; The recommended file extension for the source file of a JSP page is.jsp. The recommended extension for the source file of a fragment of a JSP page is.jspf. Encapsulates a portion of JSP code in an object that can be invoked as many times as needed. 7

JSP Page Kinds of tags: <%= %> 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 (e.g.html file) or a package (e.g. java.sql.*). e.g. <%@ page contenttype="text/html; charset=utf-8" %> e.g. <%@ taglib uri="http://java.sun.com/jsp/jstl/core " prefix="c" %> 8

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> 9

JSP Example JSP File: hello.jsp <%@ 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> 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; %> <%@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> 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; %> <%@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>"); 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; %> <jsp:usebean id="hello" scope="session" class="greetings.hellobean" /> <jsp:setproperty name="hello" property="name" value='<%= request.getparameter <jsp:usebean ("name") >%> /> <jsp:setproperty is a standard element name="hello" that creates property="email" an object containing a collection of 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 Google( what = hello.getemail(); is a bean class? ) out.println ("<h3>hello, your name is " + name); out.println (" and your email address is " + email + ".</h3>"); %> </body></html> 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 ("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> 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: <%@ 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> 16

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. 17

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 All variables have accessor (get) and mutator (set) methods. 18

JSP 19

Let us Revisit the WelcomeServlet 20

Here is equivalent in JSP (welcome.jsp) 21

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> 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 Elements: JSP directives 24

JSP Elements: JSP directives <%@ taglib uri="http://www.example.com/custlib" prefix="mytag" %> <html> <body> <mytag:hello/> </body> </html> http://www.tutorialspoint.com/jsp/taglib_directive.htm 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

Assignment 1 64

Appendix JSP Standard Tag Library (JSTL) AND JSP Custom Tags 65

JSP Standard Tag Library (JSTL) 66

JSP Standard Tag Library (JSTL) 67

JSP Standard Tag Library (JSTL) 68

JSP Standard Tag Library (JSTL) 69

JSP Standard Tag Library (JSTL) 70

JSTL Basics: Looping collections 71

JSTL Basics: Looping collections 72

JSTL Basics: Looping collections 73

JSTL Basics: Conditional output 74

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

JSTL Basics: Using <c:set> 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 Example: More Details: http://www.tutorialspoint.com/jsp/jsp_custom_tags.htm http://docs.oracle.com/javaee/5/tutorial/doc/bnalj.html 80

81