Вы находитесь на странице: 1из 36

6

Creating the Web Tier Using


JavaServer Pages

Copyright © 2007, Oracle. All rights reserved.


Objectives

After completing this lesson, you should be able to do


the following:
• Describe the relationship between JavaServer
Pages (JSP) and servlets
• List implicit objects on JSP pages
• Describe the semantics of JSP tags
• Create a JSP segment
• Explain the use of JSP tag files
• Run and debug a JSP-based application

6-2 Copyright © 2007, Oracle. All rights reserved.


JavaServer Pages

Request Connects to

JSP
Database
Client
Generates

Dynamic content

Response EJB

6-3 Copyright © 2007, Oracle. All rights reserved.


Comparing Servlets and JSPs

Servlets: JavaServer Pages:


• Are Java programs • Are HTML pages
with embedded with embedded Java
HTML code code or pure XML
• Generate dynamic • Generate dynamic
content content
• Do not separate • Separate static and
static and dynamic dynamic content
content

6-4 Copyright © 2007, Oracle. All rights reserved.


Invoking JSPs

HTML
Invoke

JSP

Servlet

JSP

6-5 Copyright © 2007, Oracle. All rights reserved.


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>

6-6 Copyright © 2007, Oracle. All rights reserved.


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();
}
...

6-7 Copyright © 2007, 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.)

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


JSP Life Cycle

Java EE
container
Yes
OC4J First
1
time

Create servlet
http://host/date.jsp date.java
No

Compile servlet
date.class
3
Servlet life cycle

6-9 Copyright © 2007, Oracle. All rights reserved.


Basic JSP Elements

A JSP contains three main elements:


• Text elements
• Directives
• Scripting elements
– Declarations
– Expressions
– Scriptlets

6-10 Copyright © 2007, 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, not
within a method, during translation
• Are used in conjunction with expressions or
scriptlets
<%! private int i=3; %>
<%! private String a="Hello", b=" World"; %>

6-11 Copyright © 2007, Oracle. All rights reserved.


6-12 Copyright © 2007, 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 %> 1
<%= a + b %>
<%= new java.util.Date() %> 2

6-13 Copyright © 2007, 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");
%>

6-14 Copyright © 2007, Oracle. All rights reserved.


Implicit Objects

There are eight implicit objects, also known as


predefined variables, in JSP:
• request • application
• response • config
• session • pageContext
• out • page
These objects are created in the generated servlet.

6-15 Copyright © 2007, Oracle. All rights reserved.


6-16 Copyright © 2007, Oracle. All rights reserved.
Example

6-17 Copyright © 2007, Oracle. All rights reserved.


6-18 Copyright © 2007, Oracle. All rights reserved.
Directives

• Are used to set global values such as class


declarations and method implementations
• Begin with the sequence <%@
• End with the sequence %>
• Are of the following types:
– page
– include
– taglib

6-19 Copyright © 2007, Oracle. All rights reserved.


include: Example

6-20 Copyright © 2007, Oracle. All rights reserved.


page Directive

You can define the following attributes by using the


page directive:
• import • extends
• contentType • info
• isThreadSafe • errorPage
• session • isErrorPage
• buffer • language
• autoFlush

6-21 Copyright © 2007, Oracle. All rights reserved.


6-22 Copyright © 2007, Oracle. All rights reserved.
JSP and JavaBeans

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

6-23 Copyright © 2007, Oracle. All rights reserved.


Using JavaBeans with JSP

Accessing JavaBeans with the <jsp:useBean> tag:

<jsp:useBean id="myBean" scope="session"


class="lesson04.LuckyNumberBean" />

6-24 Copyright © 2007, Oracle. All rights reserved.


6-25 Copyright © 2007, Oracle. All rights reserved.
scope Attribute of <jsp:useBean> Tag
Client
Request Response

Request

Response

Forward

Page 1 Page 2 Page 3

page scope page scope page scope

request scope request scope

session scope

6-26 Copyright © 2007, Oracle. All rights reserved.


Accessing and Setting Bean Property

• Accessing bean property:


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

• Setting bean property:


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

6-27 Copyright © 2007, Oracle. All rights reserved.


6-28 Copyright © 2007, 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

6-29 Copyright © 2007, Oracle. All rights reserved.


Traditional Syntax Versus XML Syntax

Traditional: XML:
• It has no root element. • <jsp:root> is the root
element.
• page directive • <jsp:directive.
<%@ page %> page/>
• Declaration tag • <jsp:declaration>
<%! %> </jsp:declaration>
• Expression tag • <jsp:expression>
<%= expression %> </jsp:expression>
• Scriptlet • <jsp:scriptlet>
<% %> </jsp:scriptlet>

6-30 Copyright © 2007, Oracle. All rights reserved.


6-31 Copyright © 2007, Oracle. All rights reserved.
JSP Segments

• Use a JSP segment for creating a partial JSP page.


• Include one or more segments in a JSP using
<jsp:include>.
JSP segment – footer.jspf:
Copyright 2005,
<a href="http://www.oracle.com">
Oracle
</a>

JSP:

<%@ include file="/footer.jspf"%>

6-32 Copyright © 2007, Oracle. All rights reserved.


JDeveloper and JSPs

• The JSP Wizard in JDeveloper is used to create


JSPs containing skeleton code.
• Structure Pane helps to ensure that the JSP and
HTML tags are properly formatted.
• Tag Insight automatically inserts end tags after
starting a scriptlet.
• The 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

6-33 Copyright © 2007, Oracle. All rights reserved.


Summary

In this lesson, you should have learned how to:


• Identify the differences and similarities between
JSPs and servlets
• Use declarations, expressions, scriptlets, and
implicit objects on JSP pages
• Use JavaBeans with JSPs
• Create a JSP segment
• Explain the use of JSP tag files
• Run and debug a JSP-based application

6-34 Copyright © 2007, Oracle. All rights reserved.


Practice Overview:
Developing JSPs

This practice covers the following topics:


• Building a basic JSP
• Developing a JSP that uses the request and
session objects
• Building a JSP to display the results

6-35 Copyright © 2007, Oracle. All rights reserved.


6-36 Copyright © 2007, Oracle. All rights reserved.

Вам также может понравиться