Presentation is loading. Please wait.

Presentation is loading. Please wait.

8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.

Similar presentations


Presentation on theme: "8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages."— Presentation transcript:

1 8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages

2 8-2 Copyright © 2005, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Compare servlets and JavaServer Pages (JSP) Build a simple JSP Describe the JSP life cycle List the basic JSP elements Develop JSPs with declarations, expressions, and scriptlets List implicit objects Use JavaBeans with JSP

3 8-3 Copyright © 2005, Oracle. All rights reserved. JavaServer Pages Generates Dynamic content JSP Connects to EJB Client Request Database Response

4 8-4 Copyright © 2005, Oracle. All rights reserved. Comparing Servlets and JSPs Servlets: Are Java programs with embedded HTML Generate dynamic content Do not separate static and dynamic content JavaServer Pages: Are HTML pages with embedded Java code or they can be pure XML Generate dynamic content Separate static and dynamic content

5 8-5 Copyright © 2005, Oracle. All rights reserved. Invoking JSPs HTML JSP Invoke Servlet JSP

6 8-6 Copyright © 2005, Oracle. All rights reserved. The Date JSP Show Date The current time is:

7 8-7 Copyright © 2005, Oracle. All rights reserved. The Date Servlet... public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println(" "); out.println(" Show Date The current time is: "); out.println(new java.util.Date()); out.println(" "); out.close(); }...

8 8-8 Copyright © 2005, Oracle. All rights reserved. Automated JSP Features A JSP is automatically converted into a servlet the first time it is invoked: –Java source files are generated. –Java class files are generated. –The Java Just-In-Time compiler can be used. A JSP can contain extensible components: –Tags: Libraries such as OC4J JSP (OJSP) or custom-developed tags. –JavaBeans (Beans are reused and their properties are automatically introspected.)

9 8-9 Copyright © 2005, Oracle. All rights reserved. JSP Life Cycle http://host/date.jsp J2EE container Create servlet date.java Compile servlet date.class 1 Servlet life cycle First time Yes 2 3 No OC4J

10 8-10 Copyright © 2005, Oracle. All rights reserved. Basic JSP Elements A JSP contains three main elements: Text elements Directives Scripting elements –Declarations –Expressions –Scriptlets

11 8-11 Copyright © 2005, Oracle. All rights reserved. Declarations Are used to define methods or variables Begin with the sequence <%! End with the sequence %> Are inserted into the body of the servlet class during translation Are used in conjunction with expressions or scriptlets

12 8-12 Copyright © 2005, Oracle. All rights reserved. Expressions Begin with the sequence <%= Contain Java expressions that are evaluated and inserted into the servlets output End with the sequence %> Do not end with a semicolon 1 2

13 8-13 Copyright © 2005, Oracle. All rights reserved. Scriptlets Begin with the sequence <% Contain a block of Java code that is executed every time a request is made End with the sequence %> <% if (i<3) out.print("i<3"); if (i==3) out.print("i==3"); else out.print("i>3"); %>

14 8-14 Copyright © 2005, Oracle. All rights reserved. Implicit Objects There are eight implicit objects, also known as predefined variables, in JSP: request response session out application config pageContext page

15 8-15 Copyright © 2005, Oracle. All rights reserved.

16 8-16 Copyright © 2005, Oracle. All rights reserved. Example

17 8-17 Copyright © 2005, Oracle. All rights reserved.

18 8-18 Copyright © 2005, Oracle. All rights reserved. Directives Are used to set global values such as class declaration, method implementations, and so on Begin with the sequence <%@ End with the sequence %> Are of the following types: – page – include – taglib

19 8-19 Copyright © 2005, Oracle. All rights reserved. include : Example

20 8-20 Copyright © 2005, Oracle. All rights reserved. page Directive You can define the following attributes by using the page directive: import contentType isThreadSafe session buffer autoflush extends info errorPage isErrorPage language

21 8-21 Copyright © 2005, Oracle. All rights reserved.

22 8-22 Copyright © 2005, Oracle. All rights reserved. JSP and JavaBeans package lesson08; import java.lang.*; import java.util.*; public class LuckyNumberBean { private int luckyNum; public LuckyNumberBean() { luckyNum = (int) (1000 * Math.random()); } public int getLuckyNum() { return luckyNum; } public void setLuckyNum(int luckyNum) { this.luckyNum = luckyNum; }

23 8-23 Copyright © 2005, Oracle. All rights reserved. Using JavaBeans with JSP Accessing JavaBeans with the tag:

24 8-24 Copyright © 2005, Oracle. All rights reserved.

25 8-25 Copyright © 2005, Oracle. All rights reserved. scope Attribute of Tag Client Page 1 Page 2 Page 3 Request Forward Response Request Response page scope request scope session scope

26 8-26 Copyright © 2005, Oracle. All rights reserved. Accessing and Setting Bean Property Accessing bean property: Setting bean property:

27 8-27 Copyright © 2005, Oracle. All rights reserved.

28 8-28 Copyright © 2005, Oracle. All rights reserved. JSP XML Document Contains as its root element Includes only XML syntax and does not include the traditional JSP tags Can be processed directly by the JSP container Can be used with XML development tools

29 8-29 Copyright © 2005, Oracle. All rights reserved. Traditional Syntax Versus XML Syntax Traditional:XML: No root element page directive Declaration tag Expression tag Scriptlet is the root element

30 8-30 Copyright © 2005, Oracle. All rights reserved.

31 8-31 Copyright © 2005, Oracle. All rights reserved. JDeveloper and JSPs Use the JSP Wizard in JDeveloper to create JSPs containing skeleton code. The structure pane helps to ensure that the JSP and HTML tags are properly formatted. Tag Insight automatically inserts end tags after starting a scriptlet. JSP code is automatically created and recompiled. JDeveloper increases productivity while debugging JSPs: –Automatically includes source Java files such as your JavaBean source –Enables you to set breakpoints and watch expressions in JSPs

32 8-32 Copyright © 2005, Oracle. All rights reserved. Creating JSPs Visually

33 8-33 Copyright © 2005, Oracle. All rights reserved. JSP Tag Insight

34 8-34 Copyright © 2005, Oracle. All rights reserved. Summary In this lesson, you should have learned how to: Differentiate between servlets and JSPs Build a simple JSP Describe the JSP life cycle Use JSP elements and implicit objects Develop JSPs with declarations, expressions, and scriptlets Use JavaBeans with JSP

35 8-35 Copyright © 2005, Oracle. All rights reserved. Practices 8-1, 8-2, and 8-3: Overview These practices cover the following topics: Creating a JSP that counts the occurrence of each character in a given string Using JavaBean to calculate an equal discount on the total amount of purchase Creating a JSP that displays product_id, product_name, and price in the form of a table

36 8-36 Copyright © 2005, Oracle. All rights reserved.

37 8-37 Copyright © 2005, Oracle. All rights reserved.

38 8-38 Copyright © 2005, Oracle. All rights reserved.

39 8-39 Copyright © 2005, Oracle. All rights reserved.

40 8-40 Copyright © 2005, Oracle. All rights reserved.


Download ppt "8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages."

Similar presentations


Ads by Google