+ All Categories
Home > Documents > 8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.

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

Date post: 27-Mar-2015
Category:
Upload: isabella-macdonald
View: 217 times
Download: 0 times
Share this document with a friend
Popular Tags:
29
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages
Transcript
Page 1: 8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.

8Copyright © 2005, Oracle. All rights reserved.

Creating the Web Tier: JavaServer Pages

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

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

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

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

JavaServer Pages

Generates

Dynamic content

JSP

Connects to

EJB

Client

Request

Database

Response

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

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

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

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

Invoking JSPs

HTML

JSP

Invoke

Servlet

JSP

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

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

The Date JSP

<%@ page contentType="text/html;charset=WINDOWS-1252"%><html> <head><meta http-equiv="Content-Type" content="text/html; charset=WINDOWS-1252"><title> Show Date </title></head><body><h2> The current time is: </h2><p> <%= new java.util.Date() %> </p></body></html>

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

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("<html>"); out.println("<head><title>Show Date </title></head><body><h2>The current time is:</h2><p>"); out.println(new java.util.Date()); out.println("</body></html>"); out.close(); }...

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

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.)

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

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

JSP Life Cycle

http://host/date.jsp

J2EE container

Create servletdate.java

Compile servletdate.class

1

Servlet life cycle

Firsttime

Yes

2

3

No

OC4J

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

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

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

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

<%! private int i=3; %><%! private String a="Hello", b=" World"; %>

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

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

Expressions

• Begin with the sequence <%=• Contain Java expressions that are evaluated and

inserted into the servlet’s output

• End with the sequence %>• Do not end with a semicolon

<%= i+1 %><%= a + b %><%= new java.util.Date() %>

1

2

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

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");%>

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

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

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

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

Example

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

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

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

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

include: Example

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

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

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

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; }}

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

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

Using JavaBeans with JSP

Accessing JavaBeans with the <jsp:useBean> tag:

<jsp:useBean id="myBean" scope="session" class="lesson08.LuckyNumberBean" />

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

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

scope Attribute of <jsp:useBean> Tag

Client

Page 1 Page 2 Page 3

Request

Forward

Response

Request

Response

page scope page scope page scope

request scope request scope

session scope

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

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

Accessing and Setting Bean Property

• Accessing bean property:

• Setting bean property:

<jsp:getProperty name="myBean" property=“luckyNum" />

<jsp:setProperty name="myBean" property=“luckyNum" value="10" />

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

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

JSP XML Document

• Contains <jsp:root> 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

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

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

Traditional Syntax Versus XML Syntax

Traditional: XML:

• No root element

• page directive<%@ page %>

• Declaration tag<%! %>

• Expression tag<%= expression %>

• Scriptlet<% %>

• <jsp:root> is the root element

• <jsp:directive.page />

• <jsp:declaration></jsp:declaration>

• <jsp:expression></jsp:expression>

• <jsp:scriptlet></jsp:scriptlet>

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

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

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

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

Creating JSPs Visually

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

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

JSP Tag Insight

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

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

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

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


Recommended