Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.

Similar presentations


Presentation on theme: "COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week."— Presentation transcript:

1 COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week 3 http://webapps.cse.unsw.edu.au/webcms2/course/index.php?cid=2411

2 Java Servlets Review: Java Servlets 2COMP9321, 15s2, Week 3 The Request: E-Mail Form Enter your name and e-mail address. Then click the Send button to send the data to the server. Name E-Mail Address 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 Java Servlets Review: Java Servlets 3COMP9321, 15s2, Week 3 The Servlet: package echo; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class EmailServlet extends HttpServlet { doGet 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 (" "); out.println (" Hello. "); out.println (" " + name+ " "); out.println (" Your email address is " + email + " "); out.println (" "); }catch (IOException e) {System.out.println ("Servlet Exception");} }

4 Java Servlets Review: Java Servlets 4COMP9321, 15s2, Week 3 Deployment Descriptor : web.xml ------------------------------------------------------------------- EmailServlet echo.EmailServlet EmailServlet /servlet/echo.EmailServlet

5 JavaServer Pages (JSP) Technology 5COMP9321, 15s2, 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 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 6COMP9321, 15s2, Week 3 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;

7 JSP Page 7COMP9321, 15s2, 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.

8 JSP Page 8COMP9321, 15s2, Week 3 Kinds of tags: is used for expressions. e.g. is used for declarations. e.g. is used for straight Java code. e.g. 5) { … %> is used to include another file such as an HTML file or a package such as java.sql.*. e.g.

9 JSP Example 9COMP9321, 15s2, Week 3 The Request: Enter your name and email address: Name Email

10 JSP Example 10COMP9321, 15s2, Week 3 JSP File: <jsp:setProperty name="hello" property="name" value=' ‘ /> <jsp:setProperty name="hello" property="email" value=‘ ‘ /> <% name = hello.getName(); email = hello.getEmail(); out.println (" Hello, your name is " + name); out.println (" and your email address is " + email + ". "); %>

11 JSP Example 11COMP9321, 15s2, Week 3 JSP File: <jsp:setProperty name="hello" property="name" value=' ‘ /> <jsp:setProperty name="hello" property="email" value=‘ ‘ /> <% name = hello.getName(); email = hello.getEmail(); out.println (" Hello, your name is " + name); out.println (" and your email address is " + email + ". "); %> page directive page directive. sets the content type returned by the page. page directive page directive. sets the content type returned by the page.

12 JSP Example 12COMP9321, 15s2, Week 3 JSP File: <jsp:setProperty name="hello" property="name" value=' ‘ /> <jsp:setProperty name="hello" property="email" value=‘ ‘ /> <% name = hello.getName(); email = hello.getEmail(); out.println (" Hello, your name is " + name); out.println (" and your email address is " + email + ". "); %> Tag library directives. 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. Tag library directives. 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.

13 JSP Example 13COMP9321, 15s2, Week 3 JSP File: <jsp:setProperty name="hello" property="name" value=' ‘ /> <jsp:setProperty name="hello" property="email" value=‘ ‘ /> <% name = hello.getName(); email = hello.getEmail(); out.println (" Hello, your name is " + name); out.println (" and your email address is " + email + ". "); %> 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. 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.

14 JSP Example 14COMP9321, 15s2, Week 3 JSP File: <jsp:setProperty name="hello" property="name" value=' ‘ /> <jsp:setProperty name="hello" property="email" value=‘ ‘ /> <% name = hello.getName(); email = hello.getEmail(); out.println (" Hello, your name is " + name); out.println (" and your email address is " + email + ". "); %> is a standard element that sets the value of an object property. is a standard element that sets the value of an object property.

15 JSP Example 15COMP9321, 15s2, Week 3 JSP File: <jsp:setProperty name="hello" property="name" value=' ‘ /> <jsp:setProperty name="hello" property="email" value=‘ ‘ /> <% name = hello.getName(); email = hello.getEmail(); out.println (" Hello, your name is " + name); out.println (" and your email address is " + email + ". "); %> 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

16 JSP Example 16COMP9321, 15s2, 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.

17 JSP 17COMP9321, 15s2, Week 3

18 JSP 18COMP9321, 15s2, Week 3

19 JSP 19COMP9321, 15s2, Week 3

20 Let us Revisit the WelcomeServlet 20COMP9321, 15s2, Week 3

21 Here is equivalent in JSP (welcome.jsp) 21COMP9321, 15s2, Week 3

22 JSP Basics 22COMP9321, 15s2, 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

23 JSP Basics 23COMP9321, 15s2, Week 3 JSP Page JSP Elements Template Text (HTML bits…) Scripting Elements DirectiveElements Action Elements Traditional Modern EL Scripting ${…} Scriptlet Expression Declaration Comments Page Include Taglib custom Standard

24 JSP Elements: JSP directives 24COMP9321, 15s2, Week 3

25 JSP Basics 25COMP9321, 15s2, Week 3 JSP Page JSP Elements Template Text (HTML bits…) ScriptingElements Directive Elements Action Elements Traditional Modern EL Scripting ${…} Scriptlet Expression Declaration Comments Page Include Taglib custom Standard

26 JSP Elements: JSP Scripting (expression) 26COMP9321, 15s2, Week 3

27 JSP Elements: Using the implicit objects 27COMP9321, 15s2, Week 3  request: the HttpServletRequest object  response: the HttpServletResponse object  session: the HttpSession object associated with the request  out:  out: the Writer object  config:  config: the ServletCong object  application:  application: the ServletContext objectExample: JSP expressions Current time is: Server Info: Servlet Init Info: This Session ID: The value of TestParam is:

28 JSP Elements: JSP Scripting (scriptlet) 28COMP9321, 15s2, 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 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 JSP Elements: JSP Scripting (scriptlet) 29COMP9321, 15s2, Week 3 Example: Hello! The time is: Your machine's address is: " ); out.println( request.getRemoteHost()); %>

30 JSP Elements: JSP Scripting (scriptlet) 30COMP9321, 15s2, Week 3 The following three examples, generate the same output …

31 JSP Elements: JSP Scripting (scriptlet) 31COMP9321, 15s2, Week 3 Example, setting the background of a page (CoreServlet p.334)

32 JSP Elements: JSP Scripting (scriptlet) 32COMP9321, 15s2, Week 3 You can also use the scriptlet to conditionally generate HTML.

33 JSP Elements: JSP Scripting (comment) 33COMP9321, 15s2, Week 3

34 Attributes in a JSP 34COMP9321, 15s2, 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….

35 JSP Basics 35COMP9321, 15s2, Week 3 JSP Page JSP Elements Template Text (HTML bits…) Scripting Elements Directive Elements ActionElements Traditional Modern EL Scripting ${…} Scriptlet Expression Declaration Comments Page Include Taglib custom Standard

36 JSP Elements: JSP Actions 36COMP9321, 15s2, Week 3 (HeadFirst) p.309

37 JSP Elements: JSP Actions (include) 37COMP9321, 15s2, Week 3

38 jsp:include vs. include directive 38COMP9321, 15s2, Week 3 (CoreServlet p.380)

39 JSP Elements: JSP Actions (forward) 39COMP9321, 15s2, Week 3

40 JSP Elements: JSP Actions (useBean) 40COMP9321, 15s2, Week 3

41 JSP Elements: JSP Actions (useBean) 41COMP9321, 15s2, Week 3

42 JSP Elements: JSP Actions (useBean) 42COMP9321, 15s2, Week 3

43 JSP Elements: JSP Actions (useBean) 43COMP9321, 15s2, Week 3 Sharing Beans: using scope attribute

44 JSP Elements: JSP Actions (useBean) 44COMP9321, 15s2, Week 3 Sharing Beans: using scope attribute

45 JSP Basics 45COMP9321, 15s2, 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

46 Expression Language (EL) in JSP 46COMP9321, 15s2, Week 3

47 Expression Language (EL) in JSP 47COMP9321, 15s2, Week 3

48 Expression Language (EL) in JSP 48COMP9321, 15s2, Week 3 Towards Script-less JSP

49 Expression Language (EL) in JSP 49COMP9321, 15s2, Week 3 (HeadFIrst) p.367

50 Expression Language (EL) in JSP 50COMP9321, 15s2, Week 3

51 EL Basics: Accessing Scoped Variables 51COMP9321, 15s2, Week 3

52 EL Basics: Accessing Scoped Variables 52COMP9321, 15s2, Week 3

53 EL Basics: Using dot vs. Using [ ] operator 53COMP9321, 15s2, Week 3

54 EL Basics: Using dot vs. Using [ ] operator 54COMP9321, 15s2, Week 3

55 EL Basics: Using dot vs. Using [ ] operator 55COMP9321, 15s2, Week 3

56 EL Basics: Using dot vs. Using [ ] operator 56COMP9321, 15s2, Week 3

57 EL Basics: Using dot vs. Using [ ] operator 57COMP9321, 15s2, Week 3

58 EL Basics: Using dot vs. Using [ ] operator 58COMP9321, 15s2, Week 3

59 EL Basics: EL Implicit Objects 59COMP9321, 15s2, Week 3

60 EL Basics: EL Implicit Objects 60COMP9321, 15s2, Week 3

61 EL Basics: EL Implicit Objects 61COMP9321, 15s2, Week 3

62 EL Basics: EL Operators 62COMP9321, 15s2, Week 3

63 JSP Standard Tag Library (JSTL) 63COMP9321, 15s2, Week 3

64 JSP Standard Tag Library (JSTL) 64COMP9321, 15s2, Week 3

65 JSP Standard Tag Library (JSTL) 65COMP9321, 15s2, Week 3

66 JSP Standard Tag Library (JSTL) 66COMP9321, 15s2, Week 3

67 JSP Standard Tag Library (JSTL) 67COMP9321, 15s2, Week 3

68 JSTL Basics: Looping collections 68COMP9321, 15s2, Week 3

69 JSTL Basics: Looping collections 69COMP9321, 15s2, Week 3

70 JSTL Basics: Looping collections 70COMP9321, 15s2, Week 3

71 JSTL Basics: Conditional output 71COMP9321, 15s2, Week 3

72 JSTL Basics: Conditional output 72COMP9321, 15s2, Week 3 2- https://www.ibm.com/developerworks/library/j-jstl0318/

73 JSTL Basics: Using JSTL Basics: Using 73COMP9321, 15s2, Week 3

74 JSTL Basics: Using JSTL Basics: Using 74COMP9321, 15s2, Week 3

75 JSTL Basics: Working with URL 75COMP9321, 15s2, Week 3

76 Other things available in JSTL 76COMP9321, 15s2, Week 3

77 JSP Basics 77COMP9321, 15s2, 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

78 JSP Custom Tags 78COMP9321, 15s2, Week 3

79 JSP Custom Tags 79COMP9321, 15s2, Week 3

80 JSP Custom Tags 80COMP9321, 15s2, Week 3

81 JSP Custom Tags 81COMP9321, 15s2, Week 3

82 JSP Custom Tags 82COMP9321, 15s2, Week 3

83 JSP Custom Tags 83COMP9321, 15s2, Week 3

84 JSP 84COMP9321, 15s2, Week 3

85 85COMP9321, 15s2, Week 3


Download ppt "COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week."

Similar presentations


Ads by Google