Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
COMP9321 Web Application EngineeringSemester 2, 2017
Dr. Amin BeheshtiService Oriented Computing Group, CSE, UNSW Australia
Week 3
1COMP9321, 17s2, Week 3
http://webapps.cse.unsw.edu.au/webcms2/course/index.php?cid=2465
Review: Static vs. Dynamic Web Page
2COMP9321, 17s2, Week 3
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!
web page whose construction is controlled by an application server processing server-side scripts.
software framework that provides both facilities to create web applications and a server environment to run them.
is-a
is-a
Java application servers
e.g.
It's core set of API and features are defined by Java EE.
The Web modules include Java Servlets and JavaServer Pages (JSP).
http://docs.oracle.com/javaee/6/tutorial/doc/
Review: Java Servlets
3COMP9321, 17s2, Week 3
http://java.sun.com/products/servlet/index.jsphttp://docs.oracle.com/javaee/6/tutorial/doc/bnafd.html
JavaServer Pages (JSP) Technology
4COMP9321, 17s2, Week 3
JavaServer Pages (JSP) Technology
5COMP9321, 17s2, Week 3
• 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 ofJava Servlet technology; but provides a more natural approachto creating static content.
• JSP is similar to PHP, but it uses the Java programminglanguage.
• To deploy and run JavaServer Pages, a compatible web serverwith a servlet container, such as Apache Tomcat, is required.
Main Features of JSP technology
6COMP9321, 17s2, Week 3
• A language for developing JSP pages, which are text-baseddocuments that describe how to process a request andconstruct a response;
• An Expression Language (EL) for accessing server-sideobjects;
• Mechanisms for defining extensions to the JSP language;
JSP Page
7COMP9321, 17s2, Week 3
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 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.
Encapsulates a portion of JSP code in an object that can be invoked as many times as needed.
JSP Page
8COMP9321, 17s2, Week 3
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" %>
JSP Example
9COMP9321, 17s2, 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
10COMP9321, 17s2, Week 3
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>
JSP Example
11COMP9321, 17s2, 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
12COMP9321, 17s2, 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
13COMP9321, 17s2, 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.
• Google(“what is a bean class?”)
JSP Example
14COMP9321, 17s2, 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
15COMP9321, 17s2, 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
16COMP9321, 17s2, 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
17COMP9321, 17s2, 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
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
18COMP9321, 17s2, 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 All variables have accessor (get) and mutator (set) methods.
JSP
19COMP9321, 17s2, Week 3
Let us Revisit the WelcomeServlet
20COMP9321, 17s2, Week 3
Here is equivalent in JSP (welcome.jsp)
21COMP9321, 17s2, Week 3
JSP Basics
22COMP9321, 17s2, Week 3
JSP Page
JSP Elements
Template Text (HTML bits…)
ScriptingElements
DirectiveElements
ActionElements
Traditional
Modern EL Scripting${…}
ScriptletExpressionDeclarationComments
PageIncludeTaglib
custom
Standard
<abc:mytag>
<jsp:useBean><jsp:getProperty><jsp:setProperty><jsp:include><jsp:forward><jsp:param>
JSP Basics
23COMP9321, 17s2, Week 3
JSP Page
JSP Elements
Template Text (HTML bits…)
ScriptingElements
DirectiveElements
ActionElements
Traditional
Modern EL Scripting${…}
ScriptletExpressionDeclarationComments
PageIncludeTaglib
custom
Standard
<abc:mytag>
<jsp:useBean><jsp:getProperty><jsp:setProperty><jsp:include><jsp:forward><jsp:param>
JSP Elements: JSP directives
24COMP9321, 17s2, Week 3
JSP Elements: JSP directives
25COMP9321, 17s2, Week 3
http://www.tutorialspoint.com/jsp/taglib_directive.htm
<%@ taglib uri="http://www.example.com/custlib" prefix="mytag" %><html><body><mytag:hello/></body></html>
JSP Basics
26COMP9321, 17s2, Week 3
JSP Page
JSP Elements
Template Text (HTML bits…)
ScriptingElements
DirectiveElements
ActionElements
Traditional
Modern EL Scripting${…}
ScriptletExpressionDeclarationComments
PageIncludeTaglib
custom
Standard
<abc:mytag>
<jsp:useBean><jsp:getProperty><jsp:setProperty><jsp:include><jsp:forward><jsp:param>
JSP Elements: JSP Scripting (expression)
27COMP9321, 17s2, Week 3
JSP Elements: Using the implicit objects
28COMP9321, 17s2, 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)
29COMP9321, 17s2, 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)
30COMP9321, 17s2, 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)
31COMP9321, 17s2, Week 3
The following three examples, generate the same output …
JSP Elements: JSP Scripting (scriptlet)
32COMP9321, 17s2, Week 3
Example, setting the background of a page
(CoreServlet p.334)
JSP Elements: JSP Scripting (scriptlet)
33COMP9321, 17s2, Week 3
You can also use the scriptlet to conditionally generate HTML.
JSP Elements: JSP Scripting (comment)
34COMP9321, 17s2, Week 3
Attributes in a JSP
35COMP9321, 17s2, 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
36COMP9321, 17s2, Week 3
JSP Page
JSP Elements
Template Text (HTML bits…)
ScriptingElements
DirectiveElements
ActionElements
Traditional
Modern EL Scripting${…}
ScriptletExpressionDeclarationComments
PageIncludeTaglib
custom
Standard
<abc:mytag>
<jsp:useBean><jsp:getProperty><jsp:setProperty><jsp:include><jsp:forward><jsp:param>
JSP Elements: JSP Actions
37COMP9321, 17s2, Week 3
(HeadFirst) p.309
JSP Elements: JSP Actions (include)
38COMP9321, 17s2, Week 3
jsp:include vs. include directive
39COMP9321, 17s2, Week 3
(CoreServlet p.380)
JSP Elements: JSP Actions (forward)
40COMP9321, 17s2, Week 3
JSP Elements: JSP Actions (useBean)
41COMP9321, 17s2, Week 3
JSP Elements: JSP Actions (useBean)
42COMP9321, 17s2, Week 3
JSP Elements: JSP Actions (useBean)
43COMP9321, 17s2, Week 3
JSP Elements: JSP Actions (useBean)
44COMP9321, 17s2, Week 3
Sharing Beans: using scope attribute
JSP Elements: JSP Actions (useBean)
45COMP9321, 17s2, Week 3
Sharing Beans: using scope attribute
JSP Basics
46COMP9321, 17s2, Week 3
JSP Page
JSP Elements
Template Text (HTML bits…)
ScriptingElements
DirectiveElements
ActionElements
Traditional
Modern EL Scripting${…}
ScriptletExpressionDeclarationComments
PageIncludeTaglib
custom
Standard
<abc:mytag>
<jsp:useBean><jsp:getProperty><jsp:setProperty><jsp:include><jsp:forward><jsp:param>
Expression Language (EL) in JSP
47COMP9321, 17s2, Week 3
Expression Language (EL) in JSP
48COMP9321, 17s2, Week 3
Expression Language (EL) in JSP
49COMP9321, 17s2, Week 3
Towards Script-less JSP
Expression Language (EL) in JSP
50COMP9321, 17s2, Week 3
(HeadFIrst) p.367
Expression Language (EL) in JSP
51COMP9321, 17s2, Week 3
EL Basics: Accessing Scoped Variables
52COMP9321, 17s2, Week 3
EL Basics: Accessing Scoped Variables
53COMP9321, 17s2, Week 3
EL Basics: Using dot vs. Using [ ] operator
54COMP9321, 17s2, Week 3
EL Basics: Using dot vs. Using [ ] operator
55COMP9321, 17s2, Week 3
EL Basics: Using dot vs. Using [ ] operator
56COMP9321, 17s2, Week 3
EL Basics: Using dot vs. Using [ ] operator
57COMP9321, 17s2, Week 3
EL Basics: Using dot vs. Using [ ] operator
58COMP9321, 17s2, Week 3
EL Basics: Using dot vs. Using [ ] operator
59COMP9321, 17s2, Week 3
EL Basics: EL Implicit Objects
60COMP9321, 17s2, Week 3
EL Basics: EL Implicit Objects
61COMP9321, 17s2, Week 3
EL Basics: EL Implicit Objects
62COMP9321, 17s2, Week 3
EL Basics: EL Operators
63COMP9321, 17s2, Week 3
Assignment 1
64COMP9321, 17s2, Week 3
Appendix
65COMP9321, 17s2, Week 3
JSP Standard Tag Library (JSTL)
AND
JSP Custom Tags
JSP Standard Tag Library (JSTL)
66COMP9321, 17s2, Week 3
JSP Standard Tag Library (JSTL)
67COMP9321, 17s2, Week 3
JSP Standard Tag Library (JSTL)
68COMP9321, 17s2, Week 3
JSP Standard Tag Library (JSTL)
69COMP9321, 17s2, Week 3
JSP Standard Tag Library (JSTL)
70COMP9321, 17s2, Week 3
JSTL Basics: Looping collections
71COMP9321, 17s2, Week 3
JSTL Basics: Looping collections
72COMP9321, 17s2, Week 3
JSTL Basics: Looping collections
73COMP9321, 17s2, Week 3
JSTL Basics: Conditional output
74COMP9321, 17s2, Week 3
JSTL Basics: Conditional output
75COMP9321, 17s2, Week 3
2- https://www.ibm.com/developerworks/library/j-jstl0318/
JSTL Basics: Using <c:set>
76COMP9321, 17s2, Week 3
Other things available in JSTL
77COMP9321, 17s2, Week 3
JSP Basics
78COMP9321, 17s2, Week 3
JSP Page
JSP Elements
Template Text (HTML bits…)
ScriptingElements
DirectiveElements
ActionElements
Traditional
Modern EL Scripting${…}
ScriptletExpressionDeclarationComments
PageIncludeTaglib
custom
Standard
<abc:mytag>
<jsp:useBean><jsp:getProperty><jsp:setProperty><jsp:include><jsp:forward><jsp:param>
JSP Custom Tags
79COMP9321, 17s2, Week 3
JSP Custom Tags
80COMP9321, 17s2, Week 3
http://www.tutorialspoint.com/jsp/jsp_custom_tags.htm
http://docs.oracle.com/javaee/5/tutorial/doc/bnalj.html
Example:
More Details:
81COMP9321, 17s2, Week 3