J2EE Web Development Tutorial
Building J2EE Web Applications
Overview
Welcome to the lab exercises for building J2EE Web Applications. This tutorial
was originally used as the Hand's On exercises for Oracle OpenWorld 2001 in
San Francisco.
The goal of this tutorial is to provide instruction on how to build database
enabled J2EE Web applications using servlet, JSP, and EJB technologies. These
exercises are based on Oracle9i JDeveloper Release Candidate for Windows NT/2000..
Pre-requisite and Setup
- The only software component you need to run these exercises is Oracle9i
JDeveloper Release Candidate.
- Download JDeveloper from /products/jdev
- Uncompress the zip file into any directory you want to install JDeveloper
(<JDEV_HOME>)
- To start JDeveloper, run the file <JDEV_HOME>/JDev9irc/jdev/bin/jdevw.exe
- All of the labs require the deployment of web modules to an external OC4J
instance. The following steps will guide you through the process of setting
up and running a separate instance of OC4J. Note that this configuration of
OC4J is not supported.
- Copy the entire contents of <JDEV_HOME> to a separate directory(say
<JDEV_OC4J_HOME>)
- If you have JDK 1.3 setup on your machine then skip this step, otherwise
execute
<JDEV_HOME>/jdev/bin/setvars
-go
- Navigate to the <JDEV_OC4J_HOME>/j2ee/home
and execute java -jar
oc4j.jar
- Test OC4J by accessing "http://localhost:8888/" from a Web
browser.
- Once you have successfully setup your separate instance of OC4J, you
will need to create an Application Server connection in JDeveloper. This
is done by opening the "Connections" node in JDeveloper's navigator
and right-clicking "New Connection..." on the Application Server
node. In the wizard define the connection name as: "Oracle9iAS"
and accept the remaining defaults.
- Labs 3 and 4 use the "oe" or "OrderEntry" schema which
is defined in the Sample "Database Setup" workspace at: /sample_code/products/jdev/index.html
- Run the script, create_hr_oe.sql, contained this workspace as SYSTEM.
Note: The SQL script will actually install 2 sample schemas, "hr"
and "oe". Labs 3 and 4 only utilize the "oe" schema.
- Once you have created the "oe" schema, you must create a database
connection, "local_oe" for these exercises.
Note: The sample applications use a different set of database connection
names, "hr_conn" and "oe_conn". These connection names
are not needed in this exercise.
Labs
The exercises are contained in the following labs:
Exercise 1: Building and Debugging Java Servlets
Exercise 2: Building and Debugging JavaServer Pages
Exercise 3: Deploying a JSP/Servlet Application to Oracle9iAS
(OC4J)
Exercise 4: Creating a CMP entity bean and a Java client
Exercise 5: Creating a JSP client to access the EJB
Exercise 6: Deploying Your JSP-EJB Application to Oracle9iAS
(OC4J)
Exercise 7: Building a Business Components Middle Tier
Exercise 8: Creating a Simple BC4J DataBound JSP
Exercise 9: Generating a Complete BC4J JSP
Exercise 10: Deploying your BC4J JSP application to Oracle9iAS
(OC4J)
Lab 1 - Working with servlets and JSP Pages
Exercise 1: Building and Debugging Java Servlets
This exercise shows how to build and debug Java Servlets
Getting Started
- In order to get started, we must first create a workspace.
- Select File->New...{ Projects | Workspace.}. Click "Ok".
- Accept the default name for the Workspace (Workspace1), leave the "Add
a New Empty Project" checkbox checked and click "Ok".
- In the New Project dialogue, name the new project "ServletJSP".
Name the directory ...mywork\Workspace1\ServletJSP
Name the Project file name ServletJSP.jpr
- Click "Ok" to proceed.
This project will contain your servlets and JSPs.
Building your first Servlet
- To create a simple HelloWorld Servlet, select File ->New...{ Web Objects
| HttpServlet }. Click "Ok".
This wizard will guide you through the steps of creating a basic Hello World
Servlet.
- Accept all of the defaults for steps 1 through 3 of the Servlet wizard
and click "Finish".
This will generate a new file called: "Servlet1.java" along with
a J2EE web app descriptor file, "web.xml".
- Save the project, servlet and descriptor files by selecting File->Save
All.
You may also click on the Save All Icon:
on the menu.
Modifying and Running the Servlet
HttpServlets interact with Http clients (browsers) using the Request object
and the Response object.
The Request object contains all of the Http client's request information
such as the URL requested along with any HTML form parameters passed.
The Response object contains the entire response from the Servlet to the
client. The response is usually in HTML but can actually be in any document
type such as XML, or even binary such as a JPG or GIF.
In this exercise we will modify the doGet() method in the Servlet to access
specific client contained in the Request Object sent from the client.
- Edit the file, "Servlet1.java", by double-clicking on it.
- Modify the out.println statement in the doGet method
from
out.println("<p>The servlet has received a GET. This is the reply.</p>");
to
out.println("<body>Hello " + request.getRemoteHost());
out.println("<br>The browser you are using is " + request.getHeader("User-Agent"));
- Save the Servlet file Servlet1.java. (File->Save).
- Run the Servlet by right-clicking the file and and selecting "Run
Servlet1.java".
You may also click on the Run Icon:
on the menu.
- JDeveloper will compile and launch the servlet in a browser window with
a printed output of:
The servlet has received a GET. This is the reply.
Hello yourhost The browser you are using is yourbrowser
Debugging Servlets
- In the servlet code add a breakpoint to the source code line:
out.println("<br>The browser you are using is " + request.getHeader("User-Agent"));
This is done by clicking on the grey area where the line number is located
on the left of the editor. Once marked for debugging, the source code line
will be highlighted in red.
- Debug the Servlet by right-clicking the file and select "Debug Servlet1.java".
Or click:

- The Debugger should start up and stop execution at the point of the print
statement. The Browser will now be waiting for the Servlet to render.
- At this point you will be able to inspect the state of the running servlet
by accessing the new debugging windows which appear. They'll show you the
entire state of the running process.
- Press the green resume button
on the menu (or F9) to continue execution of the Servlet.
- After executing the servlet, stop the debugger by pressing the red terminate
button
(or ctrl F2). This
will shut down the debugger.
Exercise 2: Building and Debugging JavaServer Pages
JavaServer Pages are an extension of the Servlet API. The key benefit is that
they allow for a page based way of editing dynamic content. JDeveloper provides
a complete set of features for creating, editing and debugging JavaServer Pages.
This Exercise shows how to build and debug basic JavaServer Pages in JDeveloper.
Creating a Helloworld JSP page
- In the same project as before "ServletJSP.jpr", you will build
a simple "Helloworld" JSP page.
- To create a JSP, select File -> New...{ Web Objects | JSP } Click "Ok".
- You will see a JSP page creation dialogue, accept the defaults and click
"Ok".
- You should now have a new JSP file, "untitled1.jsp", added to
your project and invoked in the editor.
- Save the JSP file.
Debugging a JSP
Notice the new JSP file has the scriptlet code
<% out.println((new java.util.Date()).toString()); %>
Note: "Scriptlet" code is denoted by the characters: "<%"
and "%>". Any Java code inside of these "scriptlet" markers
is executed when the JSP page is viewed.
- Let's debug this JSP page by placing a Debug point on the line which
contains the scriptlet code.
- As before, right-click the "untitled1.jsp" file and select "Debug
untitled1.jsp" to start the JSP in Debug mode.
- The Debugger should start up and stop execution at the point of the print
statement. The Browser will now be waiting for the JSP page to render.
- Press the green resume
button to continue execution of JSP page.
- Once finished, stop the debugger by pressing the red Stop button
.
Exercise 3: Deploying a JSP/Servlet Application to Oracle9iAS
(OC4J)
Oracle9i JDeveloper has the ability to easily deploy J2EE compliant JSP/Servlet
applications to any J2EE application including Oracle9iAS (OC4J). This Exercise
demonstrates how to deploy the simple application to Oracle9iAS.
Note: Before any deployments can occur make sure your external OC4J appserver
is running.
Note: You can test this by pointing your browser to:
http://localhost:8888
To deploy our JSP/Servlet application we must first create a WAR deployment
profile.
- In the same project, select File -> New... { Deployment | J2EE Web Module
(WAR File) }. Click "Ok".
- Click "Save" to store your deployment profile in the default location.
(This is the profile, not the WAR file.)
- In the J2EE Web Module Deployment Profile Settings page, click "Ok"
to accept the defaults.
- You will now see the deployment profile, "webapp1.deploy" added
to your project.
To edit the settings of the deployment profile, you can right-click "Settings.."
on the profile. (For this exercise do, not edit the default settings.)
- To deploy your application, right-click on the deployment profile and select
"Deploy to -> Oracle9iAS".
Note:The connections to the application server and database have been preset
for you. All server connections can accessed/edited via the "Connections"
node on the navigator.
- After the successful completion of your deployment, you will see the following
message in the log window:
.
Exit status of Oracle9iAS admin tool (-bindWebApp): 0
---- Deployment finished. ---- ......
To access your JSP and Servlet, point your browser to:
http://localhost:8888/Workspace1-ServletJSP-context-root/untitled1.jsp
To see your Servlet use:
http://localhost:8888/Workspace1-ServletJSP-context-root/servlet/mypackage.Servlet1
Note: The generic format for accessing Web applications
on a J2EE server is:
http://host:port/WebContextRoot/Filename
(or http://host:port/WebContextRoot/servlet/ServletName for servlets.)
The WebContextRoot is a project setting. You can edit the project settings by
double-clicking on the project node and clicking on "Common->J2EE".
Lab 2 - Working with EJBs and JSPs
Oracle9i JDeveloper provides a thorough, fully J2EE compliant environment for
developing Enterprise JavaBeans. In this exercise we will create a container
managed Entity Bean which accesses a database table. We will then build both
a Java client and a JSP client to access the EJB. After running locally, we
will then deploy the EJB/JSP application to Oracle9iAS.
Exercise 4: Creating a CMP entity bean and a Java client
Building a Container-managed Entity Bean based on a database table
In addition to providing many ways to develop EJBs, Oracle9i JDeveloper provides
a simple way to develop a container-managed (CMP) entity bean which is based
on a database table.
To proceed, we will create a new workspace and project which will contain our
new enterprise bean and various clients.
- First, close all editor windows for the untitled1.jsp and the Servlet1.java
to clear up the work environment.
- New create a new Workspace. Select File->New...{ Projects | Workspace
}. Click "Ok".
- Accept the default name for the Workspace (Workspace2), leave the "Add
a New Empty Project" checkbox checked and click "Ok".
- In the New Project dialogue, name the new project "EJB_JSP".
Name the directory ...mywork\Workspace2\EJB_JSP
Name the Project file name EJB_JSP.jpr
- Click "Ok" to create the new project.
- Save all. File -> Save All..
Create an entity bean based on a database table:
- Click on the project EJB_JSP.jpr to highlight it.
- Select File->New...{ Enterprise JavaBeans | Container-managed Entity
Beans from Tables }. Click "Ok".
- On the first page, select the JDBC connection, "local_oe".
- Select the table, "OE.CUSTOMERS" and move it over to the "Selected
Tables" window by clicking on the ">" button.
- Click "Next" to proceed.
- On the next page accept all the defaults and click "Finish" to
generate your bean.
- You should now see your new bean added to your project.
- Save all. File->Save All..
- Compile the bean and it's associated classes by clicking on the "Customers"
bean and right-clicking "Build".
(You can also build the project by selecting, Project->Build EJB_JSP.jpr.)
Creating a Java client to access the bean
- Click on the "Customers" bean to highlight it and right-click,
"Create sample java client".
- On the next dialogue, accept the default and click "Ok" to generate
the Java client.
- You will now see the java file, "SampleCustomersClient.java" added
to the project.
Running the sample Java client
To see the Java client in action, we must first start the EJB process. Oracle9i
JDeveloper allows you to run the EJB in it's embedded OC4J server.
To start the EJB:
- Click on the "Customers" bean to select it.
- Select "Run Customers" from the context menu (right-click)
- The embedded OC4J will startup and the EJB will be deployed.
- Once the OC4J server is running you'll see the following message in the
message window.
Oracle9iAS
(2.0.0.0) Containers for J2EE initialized
- Now run the sample java client by right-clicking and select "Run SampleCustomersClient.java".
- You will see the java code first compile and execute.
- As the java client executes you will see data from the database table appear
in the message window at the bottom in the following format.
.
.
customer_id = 981
cust_first_name = Daniel
cust_last_name = Gueney
cust_address = null
phone_numbers = +86 10 012 3839
nls_language = zhs
nls_territory = CHINA
credit_limit = 200
cust_email = Daniel.Gueney@REDPOLL.COM
account_mgr_id = 149
Process exited with exit code 0
Exercise 5: Creating a JSP client to access the EJB
A JSP page can access the EJB in the same way as the sample java client. In
this exercise, we will use the same code in the Java client to access the EJB
but from a JSP. This exercise will use a precoded JSP sample to access this
EJB.
First, create a JSP page.
- In the same project as before, select File->New...{ Web Objects | JSP
} to create a JSP page, "untitled1.jsp".
Now we will take an existing JSP code sample and overwrite the entire JSP page.
- Highlight the ENTIRE contents of the JSP page you just created, untitled1.jsp.
- Now copy the entire contents of the JSP sample: ejbjsp.jsp
and place it in the new JSP.
The entire JSP should have been replaced by the sample JSP code to access
the EJB.
- Save all. File-> Save All..
- Run the JSP to see it access the EJB..
Right-click "untitled1.jsp" and select "Run untitled1.jsp".
This will compile the JSP and launch it in a browser window.
Note: You may have to click "Refresh" on the browser if at first
you see an error message.
Also, you have to make sure that your EJB is still running.
- When the JSP client accesses the EJB, you will see all of the data from
the Customers table in an HTML table.
- You may now shutdown the embedded OC4J server by selecting "Run->Terminate->Embedded
OC4J Server".
Exercise 6: Deploying Your JSP-EJB Application to Oracle9iAS
(OC4J)
Oracle9i JDeveloper has the ability to integrate an Enterprise Application
Archive (EAR) containing an EJB jar file, and a Web application WAR file. In
this exercise we will create a WAR f and EJB JAR archive deployments and combine
them into an Enterprise Archive (EAR) deployment profile. We will then deploy
the entire application (EAR) to Oracle9iAS.
Create an EJB JAR deployment profile for your EJB
- In the project, select File -> New... { Deployment | J2EE EJB Module
(EJB JAR File) }. Click "Ok".
- Click "Save" to store your deployment profile in the default location.
- In the J2EE EJB Module Deployment Profile Settings page, click "Ok"
to accept the defaults.
- You will see the new deployment profile, "ejb1.deploy" added to
your project
Create a WAR deployment profile for your JSP application - (untitled1.jsp)
- In the project, select File -> New... { Deployment | J2EE Web Module
(WAR File) }. Click "Ok".
- Click "Save" to store your deployment profile in the default location.
- In the J2EE Web Module Deployment Profile Settings page, click "Ok"
to accept the defaults.
- As before, you will see the new deployment profile, "webapp1.deploy"
added to your project.
Create an EAR deployment profile for your entire application
- In the project, select File -> New... { Deployment | J2EE Application
(EAR File) }. Click "Ok".
- Click "Save" to store your deployment profile in the default location.
- In the J2EE Application Deployment Profile Settings page, click on "Application
Assembly" to see the list of J2EE modules available for packaging.
- Click on the checkboxes for both the EJB JAR (ejb1.deploy) and Web module
WAR (webapp1.deploy) to package these archives into the EAR file.
- Click "Ok" to save your settings.
- You will now see the Enterprise Application deployment profile, "application1.deploy"
added to your project.
- Save all your files: File-> Save All.
Deploying and running the Enterprise Appliction
- To deploy your enterprise application, right-click on the deployment profile,
"application1.deploy" and select "Deploy to -> Oracle9iAS".
- After the successful completion of your deployment, you will see the following
message: Deployment Finished.
Exit status of Oracle9iAS admin tool
(-bindWebApp): 0
---- Deployment finished. ---- ....
To run the application from the remote server,
Point your browser to:
http://localhost:8888/Workspace2-EJB_JSP-context-root/untitled1.jsp
Lab 3 - Using BC4J in JSPs
Exercise 7: Building a Business Components Middle Tier
Setup
You will create a new Workspace and Business Components Project. The business
components will be based on the OrderEntry schema and will serve as the business
logic for your JSP applications.
- Create a new Workspace.
- As before, select File->New...{ Projects | Workspace }. Click "Ok".
- Accept the default name for the Workspace, "Workspace3", and leave
the "Add a New Empty Project" checkbox checked and click "Ok".
- In the New Project dialogue, name the new project "bc4j".
Name the directory ...mywork\Workspace3\bc4j
Name the Project file name bc4j.jpr
- This project will contain your Business Components middle tier code.
- Save your new project.
- Now create default Business Components for the tables: "Customers"
and "Orders".
Select the new project, "bc4j.jpr" in the navigator.
- Select File->New...{Business Components | Business Components} to invoke
the Business Components Project Wizard. Click "Ok".
- As the Wizard appears, click "Next" to advance to the next page.
- Accept the default connection, "local_oe", and click "Next".
- Accept the default package, "mypackage", and click "Next".
- Move over the "Customers" and "Orders" tables to the
"Selected:" window and select "Finish" to generate the
default Business Components.
- Save and compile the project.
File-> Save All.
Project -> Build bc4j.jpr
You now have generated a middle tier set of Java classes which can communicate
with the database.
Optional - Run the BC4J tester
To see the your newly created Business Components in action you can run the
BC4J tester.
- Open the mypackage package node on the navigator, and right-click
on the MypackageModule node and select "Test..."
- As the Tester appears click "Connect" to test your business components.
- Now double-click on the nodes on the left tree to view data in the business
components.
- To close the tester, simply close the tester window by selecting "x"
in the upper right corner..
Exercise 8: Creating a Simple BC4J DataBound JSP
In this exercise we will create a new project with a single JSP page which
displays data from a Business Component (View Object). We will also observe
some of the new JSP editing features such as JSP Insight and the JSP/HTML structure
pane.
Create a JSPclient project
Create a New Project and name it "jsps"
- File -> New... { Projects | Empty Project }. Click "Ok".
- Name the directory ...mywork\Workspace3\jsps
Name the Project file name jsps.jpr
- This project will contain your BC4J JSP pages.
Create a JSP
- Invoke the "New.." gallery again. File->New...{ Web Objects
| JSP }. Click "Ok".
- Accept the default JSP filename, "untitled1.jsp". Click "Ok".
- The JSP page will automatically be opened in the editor and the Component
Palette will also be turned on.
For easier usage of the Component Palette, place the palette in a vertical
fashion and dock it to the right side of the IDE.
- Select File-> Save All or
to save the new project and JSP file.
Adding DataBinding to the JSP
We will now add code to display data from the Business Components we created
earlier. This is done by inserting BC4J Data Tags into the JSP page using
the Component Palette
- To add BC4J Data Tags into the palette, the Component Palette must be turned
on. (From the Menu, View->Component Palette. )
- In the Component Palette page selector, select the "BC4J Connections"
option. (Should be the default.)
- Now place the cursor in the JSP file above the closing "</body></html>"
tags and enter a few carriage returns. This is where we will insert some BC4J
JSP Data tags.
Connecting a JSP to a BC4J Application Module and View Object
- Insert an "Application Module" tag by clicking on it on the Component
Palette.
- This will bring up a dialogue which allows for selection of BC4J application
module information. Accept all the defaults and click "Next".
- Accept the remaining defaults and select "Finish" to insert the
"ApplicationModule" tag onto the page.
By doing this we are identifying a BC4J Application Module to the Page. Our
next step is to identify a Datasource (a ViewObject) to the JSP page.
- To add a BC4J datasource (ViewObject) to the JSP page, place the cursor
beneath the "ApplicationModule" tag in the JSP page and click on
the "DataSource" tag on the Component Palette.
- This will invoke a dialogue which allows us to specify a BC4J View Object
to the page.
- Once the dialogue appears, select the "CustomersView" View Object
in the tree and select "Next".
- On the next page, enter the following attribute value:
- Id = customers
Leave the remaining attributes, blank and click "Finish".
- Select File-> Save All.
The JSP page will now have access to Customer View Object's data when run.
Now we must insert tags to show the data.
Displaying BC4J Data in a JSP
In order to display the View Object's data we will use 2 other tags: "RowsetIterate",
to iterate through all of the records of the View Object, and "ShowValue"
to display a specific column value.
- On the Component Palette, change the Palette page from "BC4J Connections"
to "BC4J Data Access" using the poplist.
- In the JSP file, place the cursor beneath the appmodule and datasource tags
and click on the tag: "RowsetIterate" in the Component Palette.
- As the Tag dialogue appears, select "customers" as the datasource
and click "Finish". This will insert the following code:
<jbo:RowsetIterate datasource="customers" ></jbo:RowsetIterate>
- Place your cursor before the closing </jbo:RowsetIterate> and insert
a few carrage returns. Like this:
<jbo:RowsetIterate datasource="customers" >
</jbo:RowsetIterate>
- Insert an HTML line break "<br>" in the area between
the RowsetIterate begin and end tags and then insert a "ShowValue"
tag from the Component Palette.
- The "ShowValue" tag dialogue will appear. Enter the following
attribute values:
- datasource = customers
- dataitem = CustFirstName
Note: Use the poplist on the right to select from a list
of values.
- Select "Finish" to dismiss the dialogue and insert the tag. The JSP page
should now have the following code inserted.
<jbo:ApplicationModule id=... />
<jbo:DataSource id="customers" appid=... ></jbo:DataSource>
<jbo:RowsetIterate datasource="customers" >
<br>
<jbo:ShowValue datasource="customers" dataitem="CustFirstName" ></jbo:ShowValue>
</jbo:RowsetIterate>
- Save the file as before, File -> Save All.
- To run the JSP, right-click on the untitled1.jsp node in the navigator and
select "Run untitled1.jsp".
You will first see the embedded OC4J process initializing itself in the message
window.
When the browser appears, you should see a complete list of the customers
from the database.
...
Constantin
Harrison
Manisha
Harrison
Matthias
.
.
Exercise 9: Generating a Complete BC4J JSP
Oracle9i JDeveloper also provides a quick, productive way to generate fully
functional data enabled JSP pages. The generated pages include preconfigured
BC4J Data Tags which we just worked with.
The page generation options include the basic types database operation such
as Browsing, Editing, Inserting and Querying of data.
You will now create a new set of JSP pages which perform complete browse and
edit capabilities.
- In the same project as before, "jsps.jpr" , create a "Browse
and Edit" JSP application by clicking on: File->New...{ BC4J JSP |
Browse and Edit. }. Click "Ok".
- As the dialogue appears, click "Next" to progress to the View
Object selection panel.
- Select the "CustomersView" View Object and accept the other defaults.
Click "Finish".
- You should now see a new set of JSPs added to the project. These are Component
Tags which render user-interface controls such as tables, forms etc.
- Save all the files: File-> Save All.
- Compile the "jsps.jpr" project: Project-> Build jsps.jpr.
Note: Ignore any warnings..
- Run the JSP application by right-clicking the file "CustomersView_BrowseEdit.jsp"
in the navigator and select: "Run CustomersView_BrowseEdit.jsp".
As the application starts up, you will be presented with a complete data enabled
JSP application which allows for Browsing and editing of data from a single
View Object (database table).
Shutting down...
- Once finished viewing the application, shutdown the application by selecting:
Run->Terminate Embedded OC4J Server... from the menu.
Exercise 10: Deploying your BC4J JSP application to Oracle9iAS
(OC4J)
Deploying BC4J JSP applications to Oracle9iAS has become very easy with the
new OC4J container. To deploy a BC4J JSP application to OC4J, you simply have
to click on the deployment profile which has been pre-created for you and then
deploy your application.
For this task we will deploy the "Browse and Edit" JSP application
to your external OC4J server.
Deploying a BC4J JSP Application
Deploying a BC4J JSP application is simply a matter of invoking a prebuilt
deployment profile which was created when you created your application, "jsps_jpr_War.deploy".
- Before we deploy let's examine the Deployment Profile by right-clicking
on the deployment profile, "jsps_jpr_War.deploy", and select "Settings..".
- Notice the following:
WAR File: (location) - This is where a temporary WAR file is generated on
the file system when a deployment occurs.
EAR File: (location) - This WAR file gets packaged into this EAR file before
being sent to the application server.
- Observe the other settings without changing anything and dismiss the dialogue
by selecting "Cancel".
- Now deploy the application by right clicking on the deployment profile and
select: Deploy To -> "Oracle9iAS"
- After the deployment successfully completes, you should be able to access
the deployed JSP application at:
http://localhost:8888/Workspace3-jsps-context-root/CustomersView_BrowseEdit.jsp
This concludes the J2EE Web Development Tutorial
|