COMP9321 Web Application Engineering Java Server Pages (JSP) Dr. Basem Suleiman Service Oriented Computing Group, CSE, UNSW Australia Semester 1, 2016, Week 3 http://webapps.cse.unsw.edu.au/webcms2/course/index.php?cid=2442 1
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
Review: Java Servlets 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. 3
The Servlet: Review: Java Servlets 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");} 4
Review: Java Servlets 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> 5
JavaServer Pages (JSP) Technology 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 6
Main Features of JSP technology 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 7
JSP Page 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. 8
JSP Directives 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" %> 9
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> 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; %> <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> 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; %> <%@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> 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; %> <%@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>"); 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 <jsp:usebean ("name") >%> /> <jsp:setproperty name="hello" property="email" 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 = hello.getemail(); out.println ("<h3>hello, your name is " + name); object (the bean). out.println (" and your email address is " + email + ".</h3>"); %> Each JavaServer page can be associated with a Java bean. </body></html> is a standard element that creates an object containing a collection of JavaBeans are classes that encapsulate many objects into a single 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: <%@ 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. 16
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 The constructor has no parameters o All variables have accessor (get) and mutator (set) methods. 17
JSP Lifecycle 18
Processing JSP Files 19
JSP Parsing and Compilation 20
Let us Revisit the WelcomeServlet 21
Here is equivalent in JSP (welcome.jsp) 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 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> 24
JSP Elements: JSP directives 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
JSP Standard Tag Library (JSTL) 64
JSP Standard Tag Library (JSTL) 65
JSP Standard Tag Library (JSTL) 66
JSP Standard Tag Library (JSTL) 67
JSP Standard Tag Library (JSTL) 68
JSTL Basics: Looping collections 69
JSTL Basics: Looping collections 70
JSTL Basics: Looping collections 71
JSTL Basics: Conditional output 72
JSTL Basics: Conditional output 2- https://www.ibm.com/developerworks/library/j-jstl0318/ 73
JSTL Basics: Using <c:set> 74
JSTL Basics: Using <c:set> 75
JSTL Basics: Working with URL 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 80
JSP Custom Tags 81
JSP Custom Tags 82
JSP Custom Tags 83
JSP Custom Tags 84
JSP 85
Servlet Exercise Lab Exercises (Week 4) 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) 86
Message Board 87