Building J2EE Web Applications with Eclipse

Download Report

Transcript Building J2EE Web Applications with Eclipse

Building a J2EE Web Application with Eclipse
Eclipse is an open source IDE (Integrated Development Environment)
for developing applications in Java, C/C++, HTML, Cobol, Perl, etc.
The official Eclipse web site (eclipse.org) says, “Eclipse is a kind of
universal tool platform -- an open extensible IDE for anything and
nothing in particular.”
Building a J2EE Web Application with Eclipse
Eclipse:
• Provides a consistent feature set on most platforms
• Supports more than just Java or any single language
• Open source and free, yet fully supported
• Truly extensible and configurable
• Industrial strength
Overview of a Typical Web Application
Recall the typical dynamic web application consists of a web browser,
web server, html pages, server pages, and a database
Eclipse can be used to create the HTML pages, Server Pages,
Database Tables, and test it all using a Web Server and a internal
web browser.
HTML
HTML
Web browser
Scripted page
Web server
request
database
Eclipse support for creating an Testing Web Applications
The following components are used:
Apache Jakarta Tomcat servlet engine
Derby
JavaServer Pages Standard Tag Library (JSTL)
WTP (Web Tools Project Plugin)
HTML
HTML
Web browser
Scripted page
Web server
request
database
Eclipse support for creating an Testing Web Applications
The following components will be used:
Apache Jakarta Tomcat servlet engine
Derby
JavaServer Pages Standard Tag Library (JSTL)
WTP
JavaServer Pages
Standard Tag
Library (JSTL)
HTML
HTML
Web browser
Scripted page
Derby
database
Web server
request
Internet Explorer,
Firefox, or internal
browser
Apache Jakarta Tomcat servlet engine
Building J2EE Web Applications with Eclipse
When Eclipse is started, it asks you to choose a workspace. The workspace
is the location on the filesystem (folder, directory) where your files will be
stored. Afterwards you are presented with the Welcome screen if this is your
first time using this workspace.
Building J2EE Web Applications with Eclipse
On the welcome screen there is an Overview…
Building J2EE Web Applications with Eclipse
… Tutorials
Building J2EE Web Applications with Eclipse
… Samples
Building J2EE Web Applications with Eclipse
And a section on What’s New.
Building J2EE Web Applications with Eclipse
Eclipse is divided into Perspectives and Views. A Perspective is the set of screens
and functionality for a particular development scenerio (Java, C++).
To build a Web Application, first set the perspective to J2EE by selecting Window ->
Open Perspective -> Other
Building J2EE Web Applications with Eclipse
Select J2EE.
Building a J2EE Web Application with Eclipse
Next Create a Dynamic Web Project by selecting New -> Dynamic Web Project
Building a J2EE Web Application with Eclipse
Type in a name for the Project. In this case it is FirstWebProject and press
Finish.
(May be asked to configure a Server)
Building a J2EE Web Application with Eclipse
A folder will be created for your Project, appearing under Dynamic Web Projects
Eclipse support for creating an Testing Web Applications
The following components will be used:
Apache Jakarta Tomcat servlet engine
Derby
JavaServer Pages Standard Tag Library (JSTL)
WTP
JavaServer Pages
Standard Tag
Library (JSTL)
HTML
Web browser
Scripted page
Derby
database
Web server
request
Internet Explorer,
Firefox, or internal
browser
Apache Jakarta Tomcat servlet engine
DERBY
http://db.apache.org/derby
- Relational Database implement in Java
- Open source version of IBM Cloudscape
- Small footprint
- Provides an embedded driver
Using the Derby Database for Persistent Data
Adding the Apache Derby Nature to the project makes the class files and tools for
Derby available to the Project.
Using the Derby Database for Persistent Data
Start the Derby Server by right-clicking on the Project and selecting Apache
Derby -> Start Derby Network Server. Select Ok when the dialog box appears.
Using the Derby Database for Persistent Data
To create a Database, create a script file and execute it. To create the file, rightmouse click on the project and choose New -> File
Using the Derby Database for Persistent Data
Type in the name of an sql script file.
Using the Derby Database for Persistent Data
Type the SQL for creating the table and inserting data.
Using the Derby Database for Persistent Data
To execute the script, right-mouse click on the script file, choose Apache Derby ->
Run SQL Script using ‘ij’
Using the Derby Database for Persistent Data
The console shows the results of executing the script file.
There is a Database Explorer for further manipulating the database.
Eclipse support for creating an Testing Web Applications
The following components will be used:
Apache Jakarta Tomcat servlet engine
Derby
JavaServer Pages Standard Tag Library (JSTL)
WTP
JavaServer Pages
Standard Tag
Library (JSTL)
HTML
HTML
Web browser
Scripted page
Derby
database
Web server
request
Internet Explorer,
Firefox, or internal
browser
Apache Jakarta Tomcat servlet engine
UML Class Diagram
addaccount
main
<< link >>
<< link >>
showaccounts
UML Class Diagram
addaccount
<<builds>>
addaccountToDB
<<submits>>
accountInfo
confirmanAdd
Creating HTML Pages / Server Pages
To create a HTML page, right-click on the project and select New -> HTML
Creating HTML Pages / Server Pages
Type a filename and press Finish.
Creating HTML Pages / Server Pages
Type a filename and press Finish.
Creating HTML Pages / Server Pages
Enter HTML for the main menu page.
Creating HTML Pages / Server Pages
Enter HTML for the main menu page.
Creating HTML Pages / Server Pages
To view the page in a web browser, choose right-mouse click on the editor and
choose Run As -> Run on Server. This will start Tomcat (if it is not started) and
open the file in the browser.
(May have to choose Server and select Projects to add to the Server)
Creating HTML Pages / Server Pages
The page displays in Eclipse’s internal Web browser.
Creating HTML Pages / Server Pages
To change the web browser Eclipse uses, choose Window -> Preferences ->
General -> Web Browser -> Use External browser, and choose the browser you
would like used to display the pages (IE, Firefox, etc).
Creating HTML Pages / Server Pages
Let’s create the addaccount.html
Creating HTML Pages / Server Pages
Creating HTML Pages / Server Pages
Creating HTML Pages / Server Pages
Creating HTML Pages / Server Pages
Here’s what we have so far, viewed with the internal web
browser. Selecting the Add Account hyperlink takes us to
the addaccount.html page.
Creating HTML Pages / Server Pages
Here it is viewed with Internet Explorer.
UML Class Diagram
addaccount
<<builds>>
addaccountToDB
<<submits>>
accountInfo
confirmanAdd
addaccountToDB
confirmanAdd
<<builds>>
<<submits>>
accounts
Eclipse support for creating an Testing Web Applications
The following components will be used:
Apache Jakarta Tomcat servlet engine
Derby
JavaServer Pages Standard Tag Library (JSTL)
WTP
JavaServer Pages
Standard Tag
Library (JSTL)
HTML
HTML
Web browser
Scripted page
Derby
database
Web server
request
Internet Explorer,
Firefox, or internal
browser
Apache Jakarta Tomcat servlet engine
JSTL (JavaServerPages Standard Tag Library)
The JSP Standard Tag Library (JSTL) is a collection of custom tag libraries that
implement general-purpose functionality common to Web applications, including
iteration and conditionalization, data management formatting, manipulation of XML,
and database access.
• Core Tag Library – looping, expression evaluation, basic input/output
• Formatting/Internationalization Tag Library – parsing data, such as dates.
• Database Tag Library – tags that can be used to access SQL databases
• XML Tag Library – tags can be used to access XML elements
JSTL (JavaServerPages Standard Tag Library)
Core Tag Library Example
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head>
<title>Count to 10 Example (using JSTL)</title>
</head>
<body> <c:forEach var="i" begin="1" end="10" step="1">
<c:out value="${i}" />
<br />
</c:forEach>
</body>
</html>
JSTL (JavaServerPages Standard Tag Library)
Database Tag Library
<%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<sql:query var="cities">
SELECT CITY_NAME, COUNTRY, AIRPORT FROM APP.CITIES ORDER BY \
CITY_NAME, COUNTRY
</sql:query>
<c:forEach var="city" items="${cities.rows}">
<c:out value="${city.airport}” />
<c:out value=“${city.city_name}” />
<br/>
</c:forEach>
Let’s create the addAccountToDB page. Select New -> JSP
Type in a name
Default page
Type JSTL
Here’s what we have when
viewed with the internal
web browser
. . . And with Internet Explorer