comp9321 web application engineeringcs9321/16s1/lectures/lec03/lec-03...• import custom tag...

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

Upload: others

Post on 28-Jul-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

COMP9321 Web Application Engineering

Java Server Pages (JSP)

1 COMP9321, 16s1, Week 3

http://webapps.cse.unsw.edu.au/webcms2/course/index.php?cid=2442

Dr. Basem Suleiman

Service Oriented Computing Group, CSE, UNSW Australia

Semester 1, 2016, Week 3

Page 2: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

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 COMP9321, 16s1, Week 3

Page 3: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

Review: Java Servlets

3 COMP9321, 16s1, Week 3

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.

Page 4: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

Review: Java Servlets

4 COMP9321, 16s1, Week 3

The Servlet: 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");} }

Page 5: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

Review: Java Servlets

5 COMP9321, 16s1, Week 3

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>

Page 6: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JavaServer Pages (JSP) Technology

6 COMP9321, 16s1, Week 3

• 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

Page 7: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

Main Features of JSP technology

7 COMP9321, 16s1, Week 3

• 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

Page 8: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Page

8 COMP9321, 16s1, Week 3

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.

Page 9: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP – Directives

9 COMP9321, 16s1, Week 3

• 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" %>

Page 10: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Example

10 COMP9321, 16s1, Week 3

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>

Page 11: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Example

11 COMP9321, 16s1, Week 3

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>

Page 12: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Example

12 COMP9321, 16s1, Week 3

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>

<%@page ... %>

• page directive.

• sets the content type returned by the page.

Page 13: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Example

13 COMP9321, 16s1, Week 3

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>

<%@taglib ... %>

• Tag library directives.

• import custom tag libraries.

JavaServer Pages Standard Tag Library (JSTL):

• JSTL extends the JSP specification by adding a tag library

of JSP tags for common tasks, such as conditional

execution, loops, and database access.

Page 14: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Example

14 COMP9321, 16s1, Week 3

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>

<jsp:useBean …> • is a standard element that creates an object containing a collection of

locales and initializes an identifier that points to that object.

• is used to locate or instantiate a bean class.

• JavaBeans are classes that encapsulate many objects into a single

object (the bean).

• Each JavaServer page can be associated with a Java bean.

Page 15: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Example

15 COMP9321, 16s1, Week 3

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>

<jsp:setProperty …>

• is a standard element that sets the value of an object property.

Page 16: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Example

16 COMP9321, 16s1, Week 3

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>

Some reserved words (JSP Objects):

• request – an instance of HttpServletRequest.

• response – an instance of HttpServletResponse.

• out – a PrintWriter object for the response.

• session – the HttpSession object associated with the session.

• application – an instance of ServletContext

Page 17: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Example

17 COMP9321, 16s1, Week 3

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.

Page 18: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Lifecycle

18 COMP9321, 16s1, Week 3

Page 19: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

Processing JSP Files

19 COMP9321, 16s1, Week 3

Page 20: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Parsing and Compilation

20 COMP9321, 16s1, Week 3

Page 21: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

Let us Revisit the WelcomeServlet

21 COMP9321, 16s1, Week 3

Page 22: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

Here is equivalent in JSP (welcome.jsp)

22 COMP9321, 16s1, Week 3

Page 23: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Basics

23 COMP9321, 16s1, Week 3

JSP Page

JSP Elements

Template Text (HTML bits…)

Scripting

Elements

Directive

Elements

Action

Elements

Traditional

Modern EL Scripting

${…}

Scriptlet

Expression

Declaration

Comments

Page

Include

Taglib

custom

Standard

<abc:mytag>

<jsp:useBean>

<jsp:getProperty>

<jsp:setProperty>

<jsp:include>

<jsp:forward>

<jsp:param>

Page 24: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Basics

24 COMP9321, 16s1, Week 3

JSP Page

JSP Elements

Template Text (HTML bits…)

Scripting

Elements

Directive

Elements

Action

Elements

Traditional

Modern EL Scripting

${…}

Scriptlet

Expression

Declaration

Comments

Page

Include

Taglib

custom

Standard

<abc:mytag>

<jsp:useBean>

<jsp:getProperty>

<jsp:setProperty>

<jsp:include>

<jsp:forward>

<jsp:param>

Page 25: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP directives

25 COMP9321, 16s1, Week 3

Page 26: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Basics

26 COMP9321, 16s1, Week 3

JSP Page

JSP Elements

Template Text (HTML bits…)

Scripting

Elements

Directive

Elements

Action

Elements

Traditional

Modern EL Scripting

${…}

Scriptlet

Expression

Declaration

Comments

Page

Include

Taglib

custom

Standard

<abc:mytag>

<jsp:useBean>

<jsp:getProperty>

<jsp:setProperty>

<jsp:include>

<jsp:forward>

<jsp:param>

Page 27: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP Scripting (expression)

27 COMP9321, 16s1, Week 3

Page 28: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: Using the implicit objects

28 COMP9321, 16s1, Week 3

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>

Page 29: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP Scripting (scriptlet)

29 COMP9321, 16s1, Week 3

• 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'.

Page 30: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP Scripting (scriptlet)

30 COMP9321, 16s1, Week 3

Example:

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

Page 31: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP Scripting (scriptlet)

31 COMP9321, 16s1, Week 3

The following three examples, generate the same output …

Page 32: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP Scripting (scriptlet)

32 COMP9321, 16s1, Week 3

Example, setting the background of a page

(CoreServlet p.334)

Page 33: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP Scripting (scriptlet)

33 COMP9321, 16s1, Week 3

You can also use the scriptlet to conditionally generate HTML.

Page 34: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP Scripting (comment)

34 COMP9321, 16s1, Week 3

Page 35: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

Attributes in a JSP

35 COMP9321, 16s1, Week 3

(HeadFirst) p.309

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

Page 36: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Basics

36 COMP9321, 16s1, Week 3

JSP Page

JSP Elements

Template Text (HTML bits…)

Scripting

Elements

Directive

Elements

Action

Elements

Traditional

Modern EL Scripting

${…}

Scriptlet

Expression

Declaration

Comments

Page

Include

Taglib

custom

Standard

<abc:mytag>

<jsp:useBean>

<jsp:getProperty>

<jsp:setProperty>

<jsp:include>

<jsp:forward>

<jsp:param>

Page 37: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP Actions

37 COMP9321, 16s1, Week 3

(HeadFirst) p.309

Page 38: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP Actions (include)

38 COMP9321, 16s1, Week 3

Page 39: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

jsp:include vs. include directive

39 COMP9321, 16s1, Week 3

(CoreServlet p.380)

Page 40: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP Actions (forward)

40 COMP9321, 16s1, Week 3

Page 41: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP Actions (useBean)

41 COMP9321, 16s1, Week 3

Page 42: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP Actions (useBean)

42 COMP9321, 16s1, Week 3

Page 43: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP Actions (useBean)

43 COMP9321, 16s1, Week 3

Page 44: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP Actions (useBean)

44 COMP9321, 16s1, Week 3

Sharing Beans: using scope attribute

Page 45: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Elements: JSP Actions (useBean)

45 COMP9321, 16s1, Week 3

Sharing Beans: using scope attribute

Page 46: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Basics

46 COMP9321, 16s1, Week 3

JSP Page

JSP Elements

Template Text (HTML bits…)

Scripting

Elements

Directive

Elements

Action

Elements

Traditional

Modern EL Scripting

${…}

Scriptlet

Expression

Declaration

Comments

Page

Include

Taglib

custom

Standard

<abc:mytag>

<jsp:useBean>

<jsp:getProperty>

<jsp:setProperty>

<jsp:include>

<jsp:forward>

<jsp:param>

Page 47: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

Expression Language (EL) in JSP

47 COMP9321, 16s1, Week 3

Page 48: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

Expression Language (EL) in JSP

48 COMP9321, 16s1, Week 3

Page 49: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

Expression Language (EL) in JSP

49 COMP9321, 16s1, Week 3

Towards Script-less JSP

Page 50: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

Expression Language (EL) in JSP

50 COMP9321, 16s1, Week 3

(HeadFIrst) p.367

Page 51: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

Expression Language (EL) in JSP

51 COMP9321, 16s1, Week 3

Page 52: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

EL Basics: Accessing Scoped Variables

52 COMP9321, 16s1, Week 3

Page 53: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

EL Basics: Accessing Scoped Variables

53 COMP9321, 16s1, Week 3

Page 54: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

EL Basics: Using dot vs. Using [ ] operator

54 COMP9321, 16s1, Week 3

Page 55: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

EL Basics: Using dot vs. Using [ ] operator

55 COMP9321, 16s1, Week 3

Page 56: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

EL Basics: Using dot vs. Using [ ] operator

56 COMP9321, 16s1, Week 3

Page 57: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

EL Basics: Using dot vs. Using [ ] operator

57 COMP9321, 16s1, Week 3

Page 58: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

EL Basics: Using dot vs. Using [ ] operator

58 COMP9321, 16s1, Week 3

Page 59: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

EL Basics: Using dot vs. Using [ ] operator

59 COMP9321, 16s1, Week 3

Page 60: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

EL Basics: EL Implicit Objects

60 COMP9321, 16s1, Week 3

Page 61: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

EL Basics: EL Implicit Objects

61 COMP9321, 16s1, Week 3

Page 62: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

EL Basics: EL Implicit Objects

62 COMP9321, 16s1, Week 3

Page 63: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

EL Basics: EL Operators

63 COMP9321, 16s1, Week 3

Page 64: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Standard Tag Library (JSTL)

64 COMP9321, 16s1, Week 3

Page 65: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Standard Tag Library (JSTL)

65 COMP9321, 16s1, Week 3

Page 66: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Standard Tag Library (JSTL)

66 COMP9321, 16s1, Week 3

Page 67: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Standard Tag Library (JSTL)

67 COMP9321, 16s1, Week 3

Page 68: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Standard Tag Library (JSTL)

68 COMP9321, 16s1, Week 3

Page 69: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSTL Basics: Looping collections

69 COMP9321, 16s1, Week 3

Page 70: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSTL Basics: Looping collections

70 COMP9321, 16s1, Week 3

Page 71: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSTL Basics: Looping collections

71 COMP9321, 16s1, Week 3

Page 72: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSTL Basics: Conditional output

72 COMP9321, 16s1, Week 3

Page 73: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSTL Basics: Conditional output

73 COMP9321, 16s1, Week 3

2- https://www.ibm.com/developerworks/library/j-jstl0318/

Page 74: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSTL Basics: Using <c:set>

74 COMP9321, 16s1, Week 3

Page 75: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSTL Basics: Using <c:set>

75 COMP9321, 16s1, Week 3

Page 76: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSTL Basics: Working with URL

76 COMP9321, 16s1, Week 3

Page 77: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

Other things available in JSTL

77 COMP9321, 16s1, Week 3

Page 78: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Basics

78 COMP9321, 16s1, Week 3

JSP Page

JSP Elements

Template Text (HTML bits…)

Scripting

Elements

Directive

Elements

Action

Elements

Traditional

Modern EL Scripting

${…}

Scriptlet

Expression

Declaration

Comments

Page

Include

Taglib

custom

Standard

<abc:mytag>

<jsp:useBean>

<jsp:getProperty>

<jsp:setProperty>

<jsp:include>

<jsp:forward>

<jsp:param>

Page 79: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Custom Tags

79 COMP9321, 16s1, Week 3

Page 80: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Custom Tags

80 COMP9321, 16s1, Week 3

Page 81: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Custom Tags

81 COMP9321, 16s1, Week 3

Page 82: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Custom Tags

82 COMP9321, 16s1, Week 3

Page 83: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Custom Tags

83 COMP9321, 16s1, Week 3

Page 84: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP Custom Tags

84 COMP9321, 16s1, Week 3

Page 85: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

JSP

85 COMP9321, 16s1, Week 3

Page 86: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

Lab Exercises (Week 4)

86 COMP9321, 16s1, Week 3

Servlet Exercise • 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)

Page 87: COMP9321 Web Application Engineeringcs9321/16s1/lectures/lec03/Lec-03...• import custom tag libraries. JavaServer Pages Standard Tag Library (JSTL): JSTL extends the JSP specification

87 COMP9321, 16s1, Week 3

Message Board