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
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
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.
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");} }
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>
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
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
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.
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" %>
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>
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>
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.
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.
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.
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.
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
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.
JSP Lifecycle
18 COMP9321, 16s1, Week 3
Processing JSP Files
19 COMP9321, 16s1, Week 3
JSP Parsing and Compilation
20 COMP9321, 16s1, Week 3
Let us Revisit the WelcomeServlet
21 COMP9321, 16s1, Week 3
Here is equivalent in JSP (welcome.jsp)
22 COMP9321, 16s1, Week 3
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>
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>
JSP Elements: JSP directives
25 COMP9321, 16s1, Week 3
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>
JSP Elements: JSP Scripting (expression)
27 COMP9321, 16s1, Week 3
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>
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'.
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>
JSP Elements: JSP Scripting (scriptlet)
31 COMP9321, 16s1, Week 3
The following three examples, generate the same output …
JSP Elements: JSP Scripting (scriptlet)
32 COMP9321, 16s1, Week 3
Example, setting the background of a page
(CoreServlet p.334)
JSP Elements: JSP Scripting (scriptlet)
33 COMP9321, 16s1, Week 3
You can also use the scriptlet to conditionally generate HTML.
JSP Elements: JSP Scripting (comment)
34 COMP9321, 16s1, Week 3
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….
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>
JSP Elements: JSP Actions
37 COMP9321, 16s1, Week 3
(HeadFirst) p.309
JSP Elements: JSP Actions (include)
38 COMP9321, 16s1, Week 3
jsp:include vs. include directive
39 COMP9321, 16s1, Week 3
(CoreServlet p.380)
JSP Elements: JSP Actions (forward)
40 COMP9321, 16s1, Week 3
JSP Elements: JSP Actions (useBean)
41 COMP9321, 16s1, Week 3
JSP Elements: JSP Actions (useBean)
42 COMP9321, 16s1, Week 3
JSP Elements: JSP Actions (useBean)
43 COMP9321, 16s1, Week 3
JSP Elements: JSP Actions (useBean)
44 COMP9321, 16s1, Week 3
Sharing Beans: using scope attribute
JSP Elements: JSP Actions (useBean)
45 COMP9321, 16s1, Week 3
Sharing Beans: using scope attribute
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>
Expression Language (EL) in JSP
47 COMP9321, 16s1, Week 3
Expression Language (EL) in JSP
48 COMP9321, 16s1, Week 3
Expression Language (EL) in JSP
49 COMP9321, 16s1, Week 3
Towards Script-less JSP
Expression Language (EL) in JSP
50 COMP9321, 16s1, Week 3
(HeadFIrst) p.367
Expression Language (EL) in JSP
51 COMP9321, 16s1, Week 3
EL Basics: Accessing Scoped Variables
52 COMP9321, 16s1, Week 3
EL Basics: Accessing Scoped Variables
53 COMP9321, 16s1, Week 3
EL Basics: Using dot vs. Using [ ] operator
54 COMP9321, 16s1, Week 3
EL Basics: Using dot vs. Using [ ] operator
55 COMP9321, 16s1, Week 3
EL Basics: Using dot vs. Using [ ] operator
56 COMP9321, 16s1, Week 3
EL Basics: Using dot vs. Using [ ] operator
57 COMP9321, 16s1, Week 3
EL Basics: Using dot vs. Using [ ] operator
58 COMP9321, 16s1, Week 3
EL Basics: Using dot vs. Using [ ] operator
59 COMP9321, 16s1, Week 3
EL Basics: EL Implicit Objects
60 COMP9321, 16s1, Week 3
EL Basics: EL Implicit Objects
61 COMP9321, 16s1, Week 3
EL Basics: EL Implicit Objects
62 COMP9321, 16s1, Week 3
EL Basics: EL Operators
63 COMP9321, 16s1, Week 3
JSP Standard Tag Library (JSTL)
64 COMP9321, 16s1, Week 3
JSP Standard Tag Library (JSTL)
65 COMP9321, 16s1, Week 3
JSP Standard Tag Library (JSTL)
66 COMP9321, 16s1, Week 3
JSP Standard Tag Library (JSTL)
67 COMP9321, 16s1, Week 3
JSP Standard Tag Library (JSTL)
68 COMP9321, 16s1, Week 3
JSTL Basics: Looping collections
69 COMP9321, 16s1, Week 3
JSTL Basics: Looping collections
70 COMP9321, 16s1, Week 3
JSTL Basics: Looping collections
71 COMP9321, 16s1, Week 3
JSTL Basics: Conditional output
72 COMP9321, 16s1, Week 3
JSTL Basics: Conditional output
73 COMP9321, 16s1, Week 3
2- https://www.ibm.com/developerworks/library/j-jstl0318/
JSTL Basics: Using <c:set>
74 COMP9321, 16s1, Week 3
JSTL Basics: Using <c:set>
75 COMP9321, 16s1, Week 3
JSTL Basics: Working with URL
76 COMP9321, 16s1, Week 3
Other things available in JSTL
77 COMP9321, 16s1, Week 3
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>
JSP Custom Tags
79 COMP9321, 16s1, Week 3
JSP Custom Tags
80 COMP9321, 16s1, Week 3
JSP Custom Tags
81 COMP9321, 16s1, Week 3
JSP Custom Tags
82 COMP9321, 16s1, Week 3
JSP Custom Tags
83 COMP9321, 16s1, Week 3
JSP Custom Tags
84 COMP9321, 16s1, Week 3
JSP
85 COMP9321, 16s1, Week 3
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)
87 COMP9321, 16s1, Week 3
Message Board