Tutorial - Building BC4J
This tutorial walks you through the steps of building a simple Business Components
application based on the DEPT and EMP tables in the SCOTT schema and creating
a JSP client for the application.
Note: This tutorial requires Business Components Editor, which you can download
for free from the Oracle Technology Network at http://otn.oracle.com/products/bc4j/.
To gain access to this free resource, go to OTN's
registration page.
This tutorial is organized by the following tasks:
- Creating a Business Components Project
- Creating a Default Application
- Generating the Makefile
- Writing a JSP Client
- Deploying the JSP Client and Business Components
- Running the JSP Client
Requirements:
To work through this tutorial, you need the following:
- Oracle Business Components Editor
- Oracle8i and the SCOTT schema are required for this tutorial.
- A makefile utility (GNU Make or nmake).
- To deploy and view the JSP you will need access to Oracle.
This tutorial should take you about an hour to complete.
Creating a Business Components Project
Putting all your Business Component files into one project makes browsing packages
easier and simplifies deployment.
To create a new project:
- In the menu for the Business Component Editor, choose File | New
to create a new project.
- In the Project dialog box, change the Project name to DeptToEmpProject.
- Name the project's default Package name deptToEmp.
- Click OK.
- The Business Components Project Wizard opens.
Creating a Default Application
When you create a new Business Components Project, the Business Components Project
Wizard starts automatically. You can use this wizard to create default (generic)
Business Components which you can modify later.
To create a default application using the Business Components Project wizard:
- When the wizard opens, if the Welcome page appears, review the information
there and click Next.
- In the Connection page, establish a JDBC connection to your database. If
no connections are listed, click Add to create a new connection. The
Connection dialog box will appear.
- In the Connection Name field, enter BCED_jdbc.
- In the Username and Password fields, enter scott
and tiger, respectively. Leave the Role field Normal.
- Select Include password in deployment archive. By storing the
password in the deployment archive, you won't have to enter the password
every time you connect.
- In the Host Id field, enter the name of the database you want
to connect to. Fill in the appropriate SID and Port for
this connection. If you do not know these values, check with your database
administrator.
- Click Test Connection. A dialog box will open to indicate the
status. When the dialog box closes, you should see the message "Connection
succeeded" next to the Test Connection button. If an error occurs,
verify your settings with your database administrator.
- Click OK to close the Connection dialog box.
- On the Connection page of the wizard, click Next.
- In the Package Name page, accept the default package name (deptToEmp)
and click Next.
- In the Business Components page, select the tables you will use to create
Entity Objects.
- From the Database Schema list, select SCOTT. (If not already
selected, select the Tables checkbox.)
- In the Available list, control-click to select Dept and
Emp, and click the right-arrow (>) button to shuttle the
selected tables to the Selected list. Default Entity Objects and
Associations will be created for these database objects.
- Make sure the View Objects and View Links checkbox is selected.
This will generate default View Objects and View Links based on the selected
Entity Objects.
- Make sure the Application Module checkbox is selected. This will
create a default Application Module which you can modify later.
- Click Next.
- On the Finish page, click Finish to generate
the application. In the Navigation pane of the Business Component Editor,
notice the Business Components that have been added to your project.
To recap these steps, you did the following:
- Created a JDBC connection to your database and selected the SCOTT schema
- From the tables in that schema you chose to create Entity Objects using
Dept and Emp
- You opted to create default View Objects based on the Dept and Emp Entity
Objects, and a default Application Module
- Behind the scenes, the Business Component Editor detected a foreign key
in the Emp object and created an Association object and a corresponding View
Link to Dept (Associations and View Links can also be constructed with the
BCED wizards even if no foreign keys are detected in the database)
Generating the Makefile
The Business Component Editor allows you to create, test, and modify your
middle tier locally, and then deploy it with very few changes. Before you can
test the application locally, you need to generate and run the makefile.
To generate the makefile:
- In the Navigation pane of the Business Components Editor, click DeptToEmpProject
to make it the active node, and select Object | Generate Makefile.
- When the Generate Makefile dialog box opens, click OK. (To run the
Business Component Browser in local mode you do not need to specify a deployment
target.)
To run the makefile on Unix:
- Open a command prompt.
- Navigate to your [BCED_Home]/myprojects directory.
- Run make on the project file.
make -f DeptToEmpProject.mkx
To run the makefile on Windows NT:
- Open a command prompt.
- Navigate to your [BCED_Home]\myprojects directory.
- Run nmake on the project file.
nmake /f DeptToEmpProject.mak
When you finish running the makefile, go back to the Business Component Editor.
Don't close the terminal (DOS window) yet, you will use it again later.
Writing a JSP Client
Here you will build a JavaServer Page(JSP) HTML front-end for the application.
This is a very simple JSP which gives you a view of the EmpView View Object.
This JSP requires the oracle.css stylesheet and two Web Beans. The cascading
style sheet (CSS) facilitates the presentation of the JSP, and while you could
code the Web Beans by hand, they are included in the BCED distribution for ease
of use.
To create a JSP client:
- Create a blank HTML document.
- At the top of the page, enter the following import statement:
<%@ import = "java.util.*, oracle.jbo.*, oracle.jbo.common.ampool.*"
%>
- To facilitate presentation, you will use a style sheet. Between the <head>
tags, add the following to use the Oracle stylesheet:
<LINK REL=STYLESHEET TYPE="text/css" HREF="/webapp/css/oracle.css">
- The first Web Bean you will add is a navigation bar. Add the following lines
in the body of the page:
<jsp:useBean class="oracle.jbo.html.databeans.NavigatorBar"
id="NavBar" scope="request" >
<%
NavBar.setTargetUrl("EmpView.jsp");
NavBar.initialize(application, session, request, response, out,
"deptToEmp_DeptToEmpModule.EmpView");
NavBar.render();
%>
</jsp:useBean>
- The next Web Bean you will add is for viewing records. Add a line break
<br> and then add the following code:
<jsp:useBean class="oracle.jbo.html.databeans.ViewCurrentRecord"
id="RowViewer" scope="request">
<%
RowViewer.initialize(application, session , request, response, out, "deptToEmp_DeptToEmpModule.EmpView");
RowViewer.setReleaseApplicationResources(true);
RowViewer.render();
%>
</jsp:useBean>
- Save the file as SampleJSPClient.jsp.
Deploying the JSP and Business Components
To deploy the JSP you need to copy your JSP file, your business components,
and the web beans and CSS file used by your JSP to Oracle 9iAS, and add
the web beans and business components to the classpath.
To deploy the JSP:
- Copy your JSP file to [9iASHome]/Apache/Apache/htdocs.
- Copy the oracle.css file to [9iASHome]/Apache/Apache/htdocs.
This file is located in [BCED_Home]/doc/wh/oracle/Tutorials/webapp.zip.
- Copy [BCED_Home]/doc/wh/oracle/Tutorials/webapp.zip to [9iASHome]/Apache/BC4j/lib.
- Copy
[BCED_Home]/myclasses/DeptToEmpProject.jar to [9iASHome]/Apache/BC4j/lib.
- Open the file /Apache/Jserv/etc/jserv.properties in a
text editor.
- Find the block that begins with the comment
# OJSP environment settings
- Add the following lines to the end of the block:
wrapper.classpath=[IAS_Home]/Apache/BC4J/lib/webapp.zip
wrapper.classpath=[IAS_Home]/Apache/BC4J/lib/DeptToEmpProject.jar
Running the JSP Client
Note: To run the JSP you must use http:// not file://.
To run the JSP client:
- Restart Oracle9iAS.
- In a web browser, navigate to your Oracle9iAS server.
- Open SampleJSPClient.jsp.