Download presentation
Presentation is loading. Please wait.
Published byJack Haynes Modified over 6 years ago
1
COMP9321 Web Application Engineering Semester 2, 2017
Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 COMP9321, 17s2, Week 3
2
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 It's core set of API and features are defined by Java EE. The Web modules include Java Servlets and JavaServer Pages (JSP). COMP9321, 17s2, Week 3
3
Review: Java Servlets http://java.sun.com/products/servlet/index.jsp
COMP9321, 17s2, Week 3
4
JavaServer Pages (JSP) Technology
COMP9321, 17s2, Week 3
5
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. 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. COMP9321, 17s2, Week 3
6
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; COMP9321, 17s2, Week 3
7
JSP Page A JSP page is a text document that contains two types of text: Static data: which can be expressed in any text-based format (such as HTML, SVG, WML, and XML); JSP elements: 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. COMP9321, 17s2, Week 3
8
JSP Page <%= … %> is used for expressions.
Kinds of tags: <%= … %> is used for expressions. e.g. <%= request.getParameter (" ") %> <%! … %> is used for declarations. e.g. <%! String name, ; %> <% … %> 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=" " prefix="c" %> COMP9321, 17s2, Week 3
9
JSP Example The Request: <html> <body>
<h3>Enter your name and address: </h3> <form method="get" action="hello.jsp"> <p><input type="text" name="name" value="" size="20"/> Name </p> <p><input type="text" name=" " value="" size="20"/> </p> <p><input type="submit" name="Send" value="Send"/> </p> </form> </body> </html> COMP9321, 17s2, Week 3
10
JSP Example JSP File: hello.jsp
page contentType="text/html; charset=UTF-8" %> taglib uri=" " …%> <html> <body> <%! String name, ; %> <jsp:useBean id="hello" scope="session" class="greetings.HelloBean" /> <jsp:setProperty name="hello" property="name" value='<%= request.getParameter ("name") %>‘ /> <jsp:setProperty name="hello" property=" " value=‘<%= request.getParameter (" ") %>‘ /> <% name = hello.getName(); = hello.get (); out.println ("<h3>Hello, your name is " + name); out.println (" and your address is " + + ".</h3>"); %> </body></html> COMP9321, 17s2, Week 3
11
JSP Example <%@page ... %> page directive.
JSP File: page contentType="text/html; charset=UTF-8" %> taglib uri=" " …%> <html> <body> <%! String name, ; %> <jsp:useBean id="hello" scope="session" class="greetings.HelloBean" /> <jsp:setProperty name="hello" property="name" value='<%= request.getParameter ("name") %>‘ /> <jsp:setProperty name="hello" property=" " value=‘<%= request.getParameter (" ") %>‘ /> <% name = hello.getName(); = hello.get (); out.println ("<h3>Hello, your name is " + name); out.println (" and your address is " + + ".</h3>"); %> </body></html> ... %> page directive. sets the content type returned by the page. COMP9321, 17s2, Week 3
12
JSP Example <%@taglib ... %> Tag library directives.
JSP File: page contentType="text/html; charset=UTF-8" %> taglib uri=" " …%> <html> <body> <%! String name, ; %> <jsp:useBean id="hello" scope="session" class="greetings.HelloBean" /> <jsp:setProperty name="hello" property="name" value='<%= request.getParameter ("name") %>‘ /> <jsp:setProperty name="hello" property=" " value=‘<%= request.getParameter (" ") %>‘ /> <% name = hello.getName(); = hello.get (); out.println ("<h3>Hello, your name is " + name); out.println (" and your address is " + + ".</h3>"); %> </body></html> ... %> 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. COMP9321, 17s2, Week 3
13
<jsp:useBean …>
JSP Example JSP File: page contentType="text/html; charset=UTF-8" %> taglib uri=" " …%> <html> <body> <%! String name, ; %> <jsp:useBean id="hello" scope="session" class="greetings.HelloBean" /> <jsp:setProperty name="hello" property="name" value='<%= request.getParameter ("name") %>‘ /> <jsp:setProperty name="hello" property=" " value=‘<%= request.getParameter (" ") %>‘ /> <% name = hello.getName(); = hello.get (); out.println ("<h3>Hello, your name is " + name); out.println (" and your address is " + + ".</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?”) COMP9321, 17s2, Week 3
14
JSP Example JSP File: page contentType="text/html; charset=UTF-8" %> taglib uri=" " …%> <html> <body> <%! String name, ; %> <jsp:useBean id="hello" scope="session" class="greetings.HelloBean" /> <jsp:setProperty name="hello" property="name" value='<%= request.getParameter ("name") %>‘ /> <jsp:setProperty name="hello" property=" " value=‘<%= request.getParameter (" ") %>‘ /> <% name = hello.getName(); = hello.get (); out.println ("<h3>Hello, your name is " + name); out.println (" and your address is " + + ".</h3>"); %> </body></html> COMP9321, 17s2, Week 3
15
<jsp:setProperty …>
JSP Example JSP File: page contentType="text/html; charset=UTF-8" %> taglib uri=" " …%> <html> <body> <%! String name, ; %> <jsp:useBean id="hello" scope="session" class="greetings.HelloBean" /> <jsp:setProperty name="hello" property="name" value='<%= request.getParameter ("name") %>‘ /> <jsp:setProperty name="hello" property=" " value=‘<%= request.getParameter (" ") %>‘ /> <% name = hello.getName(); = hello.get (); out.println ("<h3>Hello, your name is " + name); out.println (" and your address is " + + ".</h3>"); %> </body></html> <jsp:setProperty …> is a standard element that sets the value of an object property. COMP9321, 17s2, Week 3
16
JSP Example JSP File: page contentType="text/html; charset=UTF-8" %> taglib uri=" " …%> <html> <body> <%! String name, ; %> <jsp:useBean id="hello" scope="session" class="greetings.HelloBean" /> <jsp:setProperty name="hello" property="name" value='<%= request.getParameter ("name") %>‘ /> <jsp:setProperty name="hello" property=" " value=‘<%= request.getParameter (" ") %>‘ /> <% name = hello.getName(); = hello.get (); out.println ("<h3>Hello, your name is " + name); out.println (" and your address is " + + ".</h3>"); %> </body></html> COMP9321, 17s2, Week 3
17
JSP Example request – an instance of HttpServletRequest.
JSP File: page contentType="text/html; charset=UTF-8" %> taglib uri=" " …%> <html> <body> <%! String name, ; %> <jsp:useBean id="hello" scope="session" class="greetings.HelloBean" /> <jsp:setProperty name="hello" property="name" value='<%= request.getParameter ("name") %>‘ /> <jsp:setProperty name="hello" property=" " value=‘<%= request.getParameter (" ") %>‘ /> <% name = hello.getName(); = hello.get (); out.println ("<h3>Hello, your name is " + name); out.println (" and your address is " + + ".</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 COMP9321, 17s2, Week 3
18
JSP Example The Bean: public class HelloBean {
private String name = ""; private String = ""; public String getName() {return name;} public String get () {return ;} public void setName (String n) {name = n;} public void set (String e) { = e;} } // HelloBean Each Java server page is associated with a Java bean. These are Java programs and reside on the server. All variables have accessor (get) and mutator (set) methods. COMP9321, 17s2, Week 3
19
JSP COMP9321, 17s2, Week 3
20
Let us Revisit the WelcomeServlet
COMP9321, 17s2, Week 3
21
Here is equivalent in JSP (welcome.jsp)
COMP9321, 17s2, Week 3
22
JSP Basics Scriptlet Expression Declaration Comments Traditional
Scripting Elements EL Scripting ${…} Modern JSP Elements Page Include Taglib Directive Elements JSP Page custom <abc:mytag> Action Elements <jsp:useBean> <jsp:getProperty> <jsp:setProperty> <jsp:include> <jsp:forward> <jsp:param> Standard Template Text (HTML bits…) COMP9321, 17s2, Week 3
23
JSP Basics Scriptlet Expression Declaration Comments Traditional
Scripting Elements EL Scripting ${…} Modern JSP Elements Page Include Taglib Directive Elements JSP Page custom <abc:mytag> Action Elements <jsp:useBean> <jsp:getProperty> <jsp:setProperty> <jsp:include> <jsp:forward> <jsp:param> Standard Template Text (HTML bits…) COMP9321, 17s2, Week 3
24
JSP Elements: JSP directives
COMP9321, 17s2, Week 3
25
JSP Elements: JSP directives
taglib uri=" prefix="mytag" %> <html> <body> <mytag:hello/> </body> </html> COMP9321, 17s2, Week 3
26
JSP Basics Scriptlet Expression Declaration Comments Traditional
Scripting Elements EL Scripting ${…} Modern JSP Elements Page Include Taglib Directive Elements JSP Page custom <abc:mytag> Action Elements <jsp:useBean> <jsp:getProperty> <jsp:setProperty> <jsp:include> <jsp:forward> <jsp:param> Standard Template Text (HTML bits…) COMP9321, 17s2, Week 3
27
JSP Elements: JSP Scripting (expression)
COMP9321, 17s2, Week 3
28
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> COMP9321, 17s2, Week 3
29
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'. COMP9321, 17s2, Week 3
30
JSP Elements: JSP Scripting (scriptlet)
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> COMP9321, 17s2, Week 3
31
JSP Elements: JSP Scripting (scriptlet)
The following three examples, generate the same output … COMP9321, 17s2, Week 3
32
JSP Elements: JSP Scripting (scriptlet)
Example, setting the background of a page (CoreServlet p.334) COMP9321, 17s2, Week 3
33
JSP Elements: JSP Scripting (scriptlet)
You can also use the scriptlet to conditionally generate HTML. COMP9321, 17s2, Week 3
34
JSP Elements: JSP Scripting (comment)
COMP9321, 17s2, Week 3
35
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 COMP9321, 17s2, Week 3
36
JSP Basics Scriptlet Expression Declaration Comments Traditional
Scripting Elements EL Scripting ${…} Modern JSP Elements Page Include Taglib Directive Elements JSP Page custom <abc:mytag> Action Elements <jsp:useBean> <jsp:getProperty> <jsp:setProperty> <jsp:include> <jsp:forward> <jsp:param> Standard Template Text (HTML bits…) COMP9321, 17s2, Week 3
37
JSP Elements: JSP Actions
(HeadFirst) p.309 COMP9321, 17s2, Week 3
38
JSP Elements: JSP Actions (include)
COMP9321, 17s2, Week 3
39
jsp:include vs. include directive
(CoreServlet p.380) COMP9321, 17s2, Week 3
40
JSP Elements: JSP Actions (forward)
COMP9321, 17s2, Week 3
41
JSP Elements: JSP Actions (useBean)
COMP9321, 17s2, Week 3
42
JSP Elements: JSP Actions (useBean)
COMP9321, 17s2, Week 3
43
JSP Elements: JSP Actions (useBean)
COMP9321, 17s2, Week 3
44
JSP Elements: JSP Actions (useBean)
Sharing Beans: using scope attribute COMP9321, 17s2, Week 3
45
JSP Elements: JSP Actions (useBean)
Sharing Beans: using scope attribute COMP9321, 17s2, Week 3
46
JSP Basics Scriptlet Expression Declaration Comments Traditional
Scripting Elements EL Scripting ${…} Modern JSP Elements Page Include Taglib Directive Elements JSP Page custom <abc:mytag> Action Elements <jsp:useBean> <jsp:getProperty> <jsp:setProperty> <jsp:include> <jsp:forward> <jsp:param> Standard Template Text (HTML bits…) COMP9321, 17s2, Week 3
47
Expression Language (EL) in JSP
COMP9321, 17s2, Week 3
48
Expression Language (EL) in JSP
COMP9321, 17s2, Week 3
49
Expression Language (EL) in JSP
Towards Script-less JSP COMP9321, 17s2, Week 3
50
Expression Language (EL) in JSP
(HeadFIrst) p.367 COMP9321, 17s2, Week 3
51
Expression Language (EL) in JSP
COMP9321, 17s2, Week 3
52
EL Basics: Accessing Scoped Variables
COMP9321, 17s2, Week 3
53
EL Basics: Accessing Scoped Variables
COMP9321, 17s2, Week 3
54
EL Basics: Using dot vs. Using [ ] operator
COMP9321, 17s2, Week 3
55
EL Basics: Using dot vs. Using [ ] operator
COMP9321, 17s2, Week 3
56
EL Basics: Using dot vs. Using [ ] operator
COMP9321, 17s2, Week 3
57
EL Basics: Using dot vs. Using [ ] operator
COMP9321, 17s2, Week 3
58
EL Basics: Using dot vs. Using [ ] operator
COMP9321, 17s2, Week 3
59
EL Basics: Using dot vs. Using [ ] operator
COMP9321, 17s2, Week 3
60
EL Basics: EL Implicit Objects
COMP9321, 17s2, Week 3
61
EL Basics: EL Implicit Objects
COMP9321, 17s2, Week 3
62
EL Basics: EL Implicit Objects
COMP9321, 17s2, Week 3
63
EL Basics: EL Operators
COMP9321, 17s2, Week 3
64
Assignment 1 COMP9321, 17s2, Week 3
65
JSP Standard Tag Library (JSTL)
Appendix JSP Standard Tag Library (JSTL) AND JSP Custom Tags COMP9321, 17s2, Week 3
66
JSP Standard Tag Library (JSTL)
COMP9321, 17s2, Week 3
67
JSP Standard Tag Library (JSTL)
COMP9321, 17s2, Week 3
68
JSP Standard Tag Library (JSTL)
COMP9321, 17s2, Week 3
69
JSP Standard Tag Library (JSTL)
COMP9321, 17s2, Week 3
70
JSP Standard Tag Library (JSTL)
COMP9321, 17s2, Week 3
71
JSTL Basics: Looping collections
COMP9321, 17s2, Week 3
72
JSTL Basics: Looping collections
COMP9321, 17s2, Week 3
73
JSTL Basics: Looping collections
COMP9321, 17s2, Week 3
74
JSTL Basics: Conditional output
COMP9321, 17s2, Week 3
75
JSTL Basics: Conditional output
2- COMP9321, 17s2, Week 3
76
JSTL Basics: Using <c:set>
COMP9321, 17s2, Week 3
77
Other things available in JSTL
COMP9321, 17s2, Week 3
78
JSP Basics Scriptlet Expression Declaration Comments Traditional
Scripting Elements EL Scripting ${…} Modern JSP Elements Page Include Taglib Directive Elements JSP Page custom <abc:mytag> Action Elements <jsp:useBean> <jsp:getProperty> <jsp:setProperty> <jsp:include> <jsp:forward> <jsp:param> Standard Template Text (HTML bits…) COMP9321, 17s2, Week 3
79
JSP Custom Tags COMP9321, 17s2, Week 3
80
JSP Custom Tags Example:
More Details: COMP9321, 17s2, Week 3
81
COMP9321, 17s2, Week 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.