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