J2EE PowerPoint Slide-3

Download Report

Transcript J2EE PowerPoint Slide-3

Java Server Pages
A
JSP page is a text-based document that contains two types of text:
static template data, which can be expressed in any text-based format,
such as HTML, SVG, WML, and XML; and JSP elements, which
construct dynamic content.
A
syntax card and reference for the JSP elements are available at:
http://java.sun.com/products/jsp/technical.html#syntax
Your First JSP Page

The Web page in Figure 11-1 is a form
that allows you to select a locale and
displays the date in a manner appropriate
to the locale.

Figure 11-1 Localized Date Form
The source code for this example is in
the j2eetutorial/examples/src/web/date
directory created when you unzip the
tutorial bundle.
The JSP page index.jsp used to create
the form appears below; it is a typical
mixture of static HTML markup and JSP
elements. If you have developed Web
pages, you are probably familiar with the
HTML document structure statements
(<head>, <body>, and so on) and the
HTML statements that create a form
(<form>) and a menu (<select>).


The source code briefing

The source code for this example is in the j2eetutorial/examples/src/web/date
directory created when you unzip the tutorial bundle. The JSP page
index.jsp used to create the form appears below; it is a typical
mixture of static HTML markup and JSP elements. If you have
developed Web pages, you are probably familiar with the HTML
document structure statements (<head>, <body>, and so on) and the
HTML statements that create a form (<form>) and a menu (<select>).
The lines in bold in the example code contain the following types
of JSP constructs:

Directives (<%@ page ... %>) import classes in the java.util package
and the MyLocales class, and set the content type returned by the
page.

The jsp:useBean element creates an object containing a collection of
locales and initializes a variable that points to that object.

Scriptlets (<% ... %> ) retrieve the value of the locale request
parameter, iterate over a collection of locale names, and
conditionally insert HTML text into the output.

Expressions (<%= ... %>) insert the value of the locale name into the
response.

The jsp:include element sends a request to another page (date.jsp)
and includes the response in the response from the calling page.
The code listing
<%@ page import="java.util.*,MyLocales" %>
<%@ page contentType="text/html; charset=ISO-8859-5" %>
<html>
<head><title>Localized Dates</title></head>
<body bgcolor="white">
<jsp:useBean id="locales" scope="application"
class="MyLocales"/>
<form name="localeForm" action="index.jsp" method="post">
<b>Locale:</b>
<select name=locale>
<%
String selectedLocale = request.getParameter("locale");
Iterator i = locales.getLocaleNames().iterator();
while (i.hasNext()) {
String locale = (String)i.next();
if (selectedLocale != null &&
selectedLocale.equals(locale)) {
%>
<option selected><%=locale%></option>
<%
} else {
%>
<option><%=locale%></option>
<%
}
}
%>
</select>
<input type="submit" name="Submit" value="Get Date">
</form>
<jsp:include page="date.jsp"/>
</body>
</html>
How to build, deploy, and execute this JSP page? 1/2

Go to j2eetutorial/examples and build the example by executing ant date (see How
to Build and Run the Examples).

Create a J2EE application called DateApp.


Select FileNewApplication.

In the file chooser, navigate to j2eetutorial/examples/src/web/date.

In the File Name field, enter DateApp.

Click New Application.

Click OK.
Create the WAR and add the Web components to the DateApp application.

Select FileNewWeb Component.

Select DateApp from the Create New WAR File In Application combo box.

Enter DateWAR in the WAR Display Name field.

Click Edit.

Navigate to j2eetutorial/examples/build/web/date. Select index.jsp,
date.jsp, MyDate.class, and MyLocales.class and click Add. Then click
Finish.

Click Next.

Click JSP In The Web Component radio button, and then click Next.

Select index.jsp from the JSP Filename combo box. Click Finish.

(Continue on next slide…)
How to build, deploy, and execute this JSP page? 2/2




Enter the context root.

Select DateApp.

Select the Web Context tab.

Enter date.
Deploy the application.

Select ToolsDeploy.

Click Finish.
Invoke the URL http://<host>:8000/date in a browser.
You will see a combo box whose entries are locales. Select a locale and click Get
Date. You will see the date expressed in a manner appropriate for that locale.
Another Example of JSP pages (Duke Bookstore)

To illustrate JSP technology, this chapter rewrites each servlet in the Duke's Bookstore application
introduced in The Example Servlets in as a JSP page. Table 11-1 lists the functions and their corresponding
JSP pages.
Table 11-1 Duke's Bookstore Example JSP Pages
Function
JSP Pages
Enter the bookstore
bookstore.jsp
Create the bookstore banner
banner.jsp
Browse the books offered for sale
catalog.jsp
Put a book in a shopping cart
catalog.jsp and bookdetails.jsp
Get detailed information on a specific book
bookdetails.jsp
Display the shopping cart
showcart.jsp
Remove one or more books from the
shopping cart
showcart.jsp
Buy the books in the shopping cart
cashier.jsp
Receive an acknowledgement for the
purchase
receipt.jsp
Code Analysis

The data for the bookstore application is still maintained in a database. However, two changes are
made to the database helper object database.BookDB.

The database helper object is rewritten to conform to JavaBeans component design patterns as
described in JavaBeans Component Design Conventions (page 270). This change is made so that JSP
pages can access the helper object using JSP language elements specific to JavaBeans components.

Instead of accessing the bookstore database directly, the helper object goes through an enterprise
bean.
The advantage of using an enterprise bean is that the helper object is no longer responsible for
connecting to the database; this job is taken over by the enterprise bean.


Furthermore, because the EJB container maintains the pool of database connections, an enterprise
bean can get a connection quicker than the helper object can.

The relevant interfaces and classes for the enterprise bean are the database.BookDBEJBHome home
interface, database.BookDBEJB remote interface, and the database.BookDBEJBImpl
implementation class, which contains all the JDBC calls to the database.

The implementation of the database helper object follows. The bean has two instance variables: the
current book and a reference to the database enterprise bean.
Code Analysis
public class BookDB {
private String bookId = "0";
private BookDBEJB database = null;
public BookDB () throws Exception {
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public void setDatabase(BookDBEJB database) {
this.database = database;
}
public BookDetails getBookDetails()
throws Exception {
try {
return (BookDetails)database.
getBookDetails(bookId);
} catch (BookNotFoundException ex) {
throw ex;
}
}
...
}


Finally, this version of the example contains an applet to generate a dynamic digital clock in the banner. See Including an
Applet for a description of the JSP element that generates HTML for downloading the applet.
The source code for the application is located in the j2eetutorial/examples/src/web/bookstore2 directory created when you
unzip the tutorial bundle (see Downloading the Examples).
How to build, deploy, and run the example: 1/2
1.
2.
3.
4.
5.
6.
7.
8.
Go to j2eetutorial/examples and build the example by running ant bookstore2.
Start the j2ee server.
Start deploytool.
Start the Cloudscape database by executing cloudscape -start.
If you have not already created the bookstore database, run ant create-web-db.
Create a J2EE application called Bookstore2App.
a.
Select FileNewApplication.
b.
In the file chooser, navigate to j2eetutorial/examples/src/web/bookstore2.
c.
In the File Name field, enter Bookstore2App.
d.
Click New Application.
e.
Click OK.
Add the Bookstore2WAR WAR to the Bookstore2App application.
a.
Select FileAddWeb WAR.
b.
In the Add Web WAR dialog box, navigate to j2eetutorial/examples/build/web/bookstore2. Select bookstore2.war. Click
Add Web WAR.
Add the BookDBEJB enterprise bean to the application.
a.
Select FileNew Enterprise Bean.
b.
Select Bookstore2App from the Create New JAR File In Application combo box.
c.
Type BookDBJAR in the JAR Display Name field.
d.
Click Edit to add the content files.
e.
In the Edit Archive Contents dialog box, navigate to the j2eetutorial/examples/build/web/ejb directory and add the
database and exception packages. Click Next.
f.
Choose Session and Stateless for the Bean Type.
g.
Select database.BookDBEJBImpl for Enterprise Bean Class.
h.
In the Remote Interfaces box, select database.BookDBEJBHome for Remote Home Interface and database.BookDBEJB
for Remote Interface.
i.
Enter BookDBEJB for Enterprise Bean Name.
j.
Click Next and then click Finish.
How to build, deploy, and run the example: 2/2
9.
10.
11.
12.
13.
14.
15.
Add a resource reference for the Cloudscape database to the BookDBEJB bean.
a.
Select the BookDBEJB enterprise bean.
b.
Select the Resource Refs tab.
c.
Click Add.
d.
Select javax.sql.DataSource from the Type column.
e.
Enter jdbc/BookDB in the Coded Name field.
Save BookDBJAR.
a.
Select BookDBJAR.
b.
Select File Save As.
c.
Navigate to the directory examples/build/web/ejb.
d.
Enter bookDB.jar in the File Name field.
e.
Click Save EJB JAR As.
Add a reference to the enterprise bean BookDBEJB.
a.
Select Bookstore2WAR.
b.
Select the EJB Refs tab.
c.
Click Add.
d.
Enter ejb/BookDBEJB in the Coded Name column.
e.
Select Session in the Type column.
f.
Select Remote in the Interfaces column.
g.
Enter database.BookDBEJBHome in the Home Interface column.
h.
Enter database.BookDBEJB in the Local/Remote Interface column.
Specify the JNDI Names.
a.
Select Bookstore2App.
b.
In the Application table, locate the EJB component and enter BookDBEJB in the JNDI Name column.
c.
In the References table, locate the EJB Ref and enter BookDBEJB in the JNDI Name column.
d.
In the References table, locate the Resource component and enter jdbc/Cloudscape in the JNDI Name column.
Enter the context root.
a.
Select the Web Context tab.
b.
Enter bookstore2.
Deploy the application.
a.
Select Tools Deploy.
b.
Click Finish.
Open the bookstore URL http://<host>:8000/bookstore2/enter.
Result