1. Trang chủ
  2. » Khoa Học Tự Nhiên

Java instantcode developing JSP and XML integrated solutions

517 71 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 517
Dung lượng 4,51 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

The application is an online retail application that allows you to develop JSP documents using XML syntaxfor JSP declarations, JSP page directives, JSP expressions, and JSPscriptlets...

Trang 1

Java InstantCode: Developing JSP and XML Integrated Solutions

SkillSoft © 2004

Use this code-intensive reference to learn about developing JSP and XML integrated solutions using XML-based JSP documents, the Java API for XML Processing (JAXP), and

Trang 2

CD Content

Trang 3

Introduction

Trang 4

The InstantCode series is designed to provide you - the developer - withcode you can use for common tasks in the workplace The goal of theInstantCode series is not to provide comprehensive information on

specific technologies - this is typically well-covered in other books

Instead, the purpose of this series is to provide actual code listings thatyou can immediately put to use in building applications for your particularrequirements

How These Books are Structured

The underlying philosophy of the InstantCode series is to present codelistings that you can download and apply to your own business needs Tosupport this, these books are divided into chapters, each covering anindependent task

Each chapter includes a brief description of the task, followed by an

overview of the element of the book's subject technology that we will use

to perform that task Each section ends with a code listing: each of theindividual code segments in the chapter is independently downloadable,

as is the complete chapter code You will be able to download sourcecode files, as well as application files

Who Should Read These Books

These books are written for software development professionals whohave basic knowledge of the associated technology and want to developcustomized technology solutions

Trang 5

This book describes J2EE solutions developed using JavaServer Pages(JSP) and Extensible Markup Language (XML) The book explains

developing JSP and XML integrated solutions using XML-based JSPdocuments, the Java API for XML Processing (JAXP), and JDOM Thebook also explains how to develop custom tags and use pre existing taglibraries for XML processing in J2EE applications In addition, the bookdescribes how to generate dynamic XML contents from J2EE applicationand integrate XML to develop internationalized Web applications

About the Author

Simanta Sarma holds a Bachelor's degree in Engineering and an

advanced diploma in Web computing He is proficient in Web, distributed,and enterprise technologies developed in J2EE He has designed andauthored numerous books, courseware and articles on J2EE

technologies

Credits

I would like to thank Radhika Chauhan, S Sripriya, and Parag Phukan forhelping me complete the book on time I also thank the editors and thequality assurance team for their timely help

Trang 6

be caused directly or indirectly by the information contained in this work

Trang 7

Based JSP Documents

Point 1: Creating Web Applications Using XML-JavaServer Pages (JSP) is a Java 2 Enterprise Edition (J2EE)

technology to develop dynamic Web applications In JSP applications, analternative to relational databases to store application data is eXtensibleMarkup Language (XML) documents XML documents provide a

libraries, such as Java Standard Tag Library (JSTL) or create your owncustom tag library to work with XML documents from JSP pages You canalso develop XML-based JSP pages, known as JSP documents to

develop Web applications

XML-based JSP documents are JSP pages written in XML syntax A JSPdocument must comply with XML specifications All JSP documents need

to have a root tag,<jsp:root>, which includes other subtags You saveJSP documents as jspx files and deploy them on J2EE application

servers

Developing XML-based JSP documents allows you to create structuredWeb pages using XML-based tools JSP documents also enable

document validation against an external Document Type Definition (DTD)

or schema to ensure consistent document structures

This chapter describes how to create a dynamic Web application usingXML-based JSP documents The application is an online retail

application that allows you to develop JSP documents using XML syntaxfor JSP declarations, JSP page directives, JSP expressions, and JSPscriptlets

Trang 8

The online retail application allows registered end-users to log on to theWeb site, view a list of all the items in the store, determine the availablequantity and price of each item, and place orders for various items Torecord the orders placed by an end-user, the application maintains a list

of orders sorted by date and time This allows end-users to view orderinformation

You can develop the online retail application by integrating the followingthree modules:

Logon module: Authenticates end-users The files in the Logonmodule are:

Logon.jspx: Displays a text field and a password field toobtain information about end-users

DoLogon.jspx: Accesses a JavaBean component toauthorize an end user to access the retailer's Web site.LogonHandler.java: Accesses a back-end database tovalidate end user information

User.java: Represents the end-user's logon informationand role An end-user of the Web site can be assignedeither the user or the administrator role

User module: Allows end-users to view item information,

purchase items, and view sales reports of all the items

purchased The user module contains the following files:

UserPage.jspx: Displays hyperlinks to the other pages ofthe user module

Trang 9

PurchaseItems.jspx: Displays the user interface that theend user uses to purchase items

AddItem.jspx: Records information about new itemsadded to the store

UpdateItem.jspx: Updates information about availableitems

ViewSales.jspx: Displays sale information about all thepurchases

ViewAllItems.jspx: Displays all the available items of thestore

Trang 10

Figure 1-1: Architecture of the Online Retail

Application

Trang 11

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[PItem]') andOBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [dbo].[PItem]

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[User]') andOBJECTPROPERTY(id, N'IsUserTable') = 1)

Trang 18

VOU= new User();

VOU.setUserID(StringUserID);

Trang 20

Creating the User Class

Trang 21

information and role Various components of the application use the Userclass to authorize an end-user and monitor the end-user during a

shopping session Listing 1-5 shows the User.java class:

Trang 30

<td width="7%">Sl No</td>

Trang 32

</tr>

</form>

Trang 34

Purchase button to make purchases, and uses hidden form fields torecord the items that an end-user purchases.

Trang 40

</div>

</td>

<td width="12%" height="21" align="right"> <div align="center">

<jsp:expression>date</jsp:expression>

</div>

</td>

<td width="14%" height="21" align="right"> <div align="center">

<jsp:expression>qty</jsp:expression>

</div>

</td>

<td width="14%" height="21" align="right"> <div align="center">

</body>

</html>

</jsp:root>

Trang 41

The above code creates the View Total Purchase page The code

connects to the database, retrieves purchase records of the end-userfrom the PItem table, and displays the retrieved records in tabular format

Figure 1-6 shows the View Total Purchase page of the online retail

application:

Figure 1-6: The View Total Purchase Page

Trang 48

<! Render HTML Form to display available items and to allow users to update and delete items Submit form data to UpdateItem.jspx >

Trang 55

<p></p>

<table rows = "2" columns = "1" width="100%" border="1">

<tr><td>The item <jsp:expression>item_name</jsp:expression>

<jsp:expression>item_qty</jsp:expression> has been

Trang 57

Connection con = DataSource_1.getConnection();

Trang 58

Statement stmt = con.createStatement();

/*Retrieve all data from pitem table as ResultSet object*/ ResultSet rs = stmt.executeQuery("select * from pitem"); int cntr1 = 0;

Trang 60

You can deploy and test the online retail application on Sun Java SystemApplication Server Platform Edition 8 The steps to deploy the applicationare:

1 Download this application server from:

http://java.sun.com/j2ee/1.4/download.html

Note To install Sun Java System Application Server Platform Edition

8, first install Java 2 Standard Edition (J2SE) 1.4.2 on yourcomputer

You can use the Microsoft SQL Server 2000 Driver for JDBC or MySQLConnector/J driver to connect to the Microsoft SQL Server or MySQLdatabases

Note Microsoft SQL Server 2000 Driver for JDBC is a type 4 driver

that connects Java applications to Microsoft SQL Server 2000databases

You can download Microsoft SQL Server 2000 Driver for JDBC from:

http://www.microsoft.com/sql/downloads/default.asp

2 After you have downloaded Microsoft SQL Server 2000 Driverfor JDBC, copy the driver Java Archive (JAR) files, msbase.jar,mssqlserver.jar, and msutil.jar, to the lib directory of Sun JavaSystem Application Server You can locate the driver JAR files

in the lib directory of the driver installation directory

3 Finally, copy the three JAR files to the lib directory of the

application server and the lib directory of the default domain ofthe application server

Note If you are using the MySQL Connector/J driver for MySQL

bin.jar file to the lib directories of the application server

database, copy the mysql-connector-java-3.0.14-production-To test the application, compile the LogonHandler.java and User.java files

Trang 62

Figure 1-10: The Connection Pools Page

5 Select the New hyperlink The Wizard Step 1 section of theCreate Connection Pool page appears Specify

SQLConnectionPool in the Name text field and select mssqlfrom the Database Vendor drop-down list, as shown in Figure 1-

11:

Figure 1-11: The Wizard Step 1 Section

6 Select the Next hyperlink The Wizard Step 2 section of theCreate Connection Pool page appears

7 Type com.microsoft.jdbcx.sqlserver.SQLServerDataSource inthe Data Source Class Name field

Note If you are using a different database, such as MySQL, you

need to specify the corresponding name of the data sourceimplementation class in the Data Source Class Name field For

Trang 63

com.mysql.jdbc.jdbc2.optional.MysqlDataSource in the DataSource Class Name field

8 Select the Next hyperlink The Wizard Step 3 section of theCreate Connection Pool page appears

9 In the Additional Properties section of the Create ConnectionPool page, specify the properties required to connect to theSQL Server 2000 database The properties that you need tospecify are:

DatabaseName: Name of the database that theapplication uses For SQL Server 2000, specifyJSPXML for this connection pool For MySQLdatabase, specify jdbc:mysql://localhost/JSPXML

ServerName: IP address of the computer where thedatabase is installed If the database is installed on thelocal computer, you can specify localhost

User: User name of the database

Password: Password for the database

10 Click the Finish hyperlink to create the connection pool

11 Select the JDBC Resources hyperlink in the left frame of AdminConsole The JDBC Resources page appears

12 Select the New hyperlink The Create JDBC Resource pageappears

13 Type jdbc/SQLResource in the JNDI Name text field and selectSQLConnectionPool from the Pool Name drop-down list, asshown in Figure 1-12:

Trang 64

1

Select Programs->Sun Microsystems->J2EE 1.4 SDK->Deploytool from the Start menu The Deployment Tool windowappears

5 Enter the name of the ear file as OnlineRetailerAppl in the FileName text box, as shown in Figure 1-13:

Trang 65

Figure 1-13: The New Application Dialog

Box

6 Click the New Application button to return to the previous NewApplication dialog box

7 Click OK to create the new application

8 Select New->Web Component from the File menu of the

Introduction dialog box appears

Deployment Tool window The New Web Application Wizard-9 Click Next The New Web Application Wizard-WAR File dialogbox appears

10 Click Edit Contents in the New Web Application Wizard-WARFile dialog box The Edit Contents of WebApp dialog box

appears

11 Select all the files of the application on the Available Files paneand click Add The Contents of WebApp pane displays theadded files, as shown in Figure 1-14:

Trang 66

Figure 1-14: The Edit Contents of WebApp Dialog

Box

12 Click OK to add the files to the Web application's Web Archive(WAR) file

13 Click Next in the New Web Application Wizard-WAR File dialogbox The New Web Application Wizard-Choose ComponentType dialog box appears

14 Select the JSP Page radio button, as shown in Figure 1-15:

Trang 67

Figure 1-15: The New Web Application Wizard-Choose

Component Type Dialog Box

15 Click Next The New Web Application Wizard-ComponentGeneral Properties dialog box appears

16 Select Logon.jspx from the JSP Filename drop-down list Thisautomatically fills the remaining fields with values Figure 1-16

shows the New Web Application Wizard-Component GeneralProperties dialog box:

Figure 1-16: The New Web Application Wizard-Component

General Properties Dialog Box

Trang 68

17 Click Next The New Web Application Wizard-Next Steps dialogbox appears.

successful deployment of the application, as shown in

Figure 1-18:

Trang 69

Figure 1-18: The Distribute Module Dialog

BoxTesting the Application

After deploying the application, you need to test the application To dothis:

1 Type the following URL in the address bar of the browser:http://localhost:8080/first/Logon.jspx

2 The logon page of the application appears Specify admin inboth the User ID and Password text fields, as shown in Figure1-19:

Figure 1-19: Specifying the User ID and

Trang 70

3 Click the Submit button The Administrator Options pageappears, as shown in Figure 1-20:

Figure 1-20: The Administrator Options

Page

4 Select the Manage Items hyperlink The Manage Items pageappears

Trang 71

6 Click the Add Item button The Manage Items page displays amessage to indicate that the item has been successfully added.The Manage Items page also displays a hyperlink to add morerecords.

7 Select the Go back to add more items hyperlink to add records

of other items

8 From the Administrator Options page, select the View Stockhyperlink The View all Items page appears This page displaysall the available items in the store, as shown in Figure 1-22:

editable text boxes along with buttons to update and deleterecords Specify new values for a record in the text field

corresponding to the Price and Quantity columns, as shown in

Figure 1-23:

Trang 72

Figure 1-23: Updating Item Records

11 Click the Update button to update a record The Update Itemspage displays a message to indicate that the record has beensuccessfully updated You can similarly click the Delete buttoncorresponding to a record to delete the record

12 On the Administrator Options page, select the Logout hyperlink

to log off from the administrator module

13 On the Logon page, specify a user name in the User ID textfield and password in the Password text field to log on to theuser module The User Options page appears, as shown in

Ngày đăng: 25/03/2019, 17:10

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN