J2EE Application Development on Apache Geronimo

Download Report

Transcript J2EE Application Development on Apache Geronimo

J2EE Application Development
on Apache Geronimo
Simplified using Eclipse
Shiva Kumar H.R.
IBM India Software Labs, Bangalore
[email protected] / [email protected]
Agenda
• Apache Geronimo - An Introduction
• Eclipse & Web Tools Platform
– Eclipse - Introduction, Java Development Tools
– Web Tools Platform, Geronimo Eclipse Plug-in
• Hands-on Exercises - 1
–
–
–
–
–
–
–
IDE Setup
Start Geronimo server
Create a new Web application with a single JSP
Deploy it on to Server
Make changes to the Web application and Redeploy
Undeploy Web application
Stop Geronimo Server
Agenda
•
Hands-on Exercises - 2: Develop a simple J2EE application
– Application Overview & Design
– Create & populate Database Tables using Admin Console.
– Create EJB project
• Create Container Managed Entity Beans using XDoclet Wizard
• XDoclet - Introduction
• Create Stateless Session Bean
– Create Web project
• Create Servlet & JSPs
– Create Enterprise Application Project and add EJB & Web projects into it.
– Create Geronimo Deployment Plan
– Deploy & Run the J2EE application
•
Hands-on Exercises - 3: Debug the J2EE application
•
Web Service Tools
Agenda
• Apache Geronimo - An Introduction
• Eclipse & Web Tools Platform
– Eclipse - Introduction, Java Development Tools
– Web Tools Platform, Geronimo Eclipse Plug-in
• Hands-on Exercises - 1
–
–
–
–
–
–
–
IDE Setup
Start Geronimo server
Create a new Web application with a single JSP
Deploy it on to Server
Make changes to the Web application and Redeploy
Undeploy Web application
Stop Geronimo Server
Apache Geronimo
• Open source J2EE application server
project developed by the Apache
Software Foundation.
• Brings together leading technologies
from the broad open source
community to support J2EE.
http://geronimo.apache.org/
• Small footprint and highly
customizable.
Geronimo as a J2EE 1.4 compliant server*
*Source: IBM developerWorks article by author Sing Li
http://www-128.ibm.com/developerworks/java/library/j-geron1/index.html
Agenda
• Apache Geronimo - An Introduction
• Eclipse & Web Tools Platform
– Eclipse - Introduction, Java Development Tools
– Web Tools Platform, Geronimo Eclipse Plug-in
• Hands-on Exercises - 1
–
–
–
–
–
–
–
IDE Setup
Start Geronimo server
Create a new Web application with a single JSP
Deploy it on to Server
Make changes to the Web application and Redeploy
Undeploy Web application
Stop Geronimo Server
Eclipse & Web Tools Platform
An Integrated development environment
(IDE) for creating, deploying, and debugging
directly against Geronimo
Eclipse - Introduction
• Eclipse is an open source community
whose projects are focused on building an
extensible development platform,
runtimes and application frameworks for
building, deploying and managing
software across the entire software
lifecycle.
• Many people know it, and hopefully love
it, as a Java IDE but Eclipse is much more
than a Java IDE.
http://www.eclipse.org/
Java Development Tools*
• Code editing features - Code completion, Syntax coloring, Error
markers & Quick fixes, Automatic indent, Code folding (expand
and collapse).
• Code refactoring - Rename fields, methods, classes, interfaces, and
packages, as well as add get/set methods for fields.
• Source-level debugging - Set absolute/conditional breakpoints, Step
into method calls as well as over them, Jump out of methods, Run to
cursor.
• Visual code browsers like Project Explorer & Hierarchy views.
*Source: Eric J. Bruno’s article “NetBeans 4.1 & Eclipse 3.1”
http://www.ddj.com/dept/java/184406194
Eclipse Web Tools Platform*
• WTP supports J2EE development.
• WTP extends Eclipse along two dimensions, namely
execution environments and artifact types.
• The execution environment dimension defines where
code runs.
– WTP extends Eclipse by adding servers as new execution
environments.
– Servers include both J2EE and Database servers.
*Source: Arthur Ryman’s article “Eclipse: The Story of Web Tools Platform 0.7”
http://java.sys-con.com/read/111212.htm
Eclipse Web Tools Platform
• The development artifact dimension defines what developers create.
– New artifact types encountered in J2EE development - HTML,
CSS, JavaScript, XHTML, JSP, XML, XSD, WSDL, SQL, and
all the J2EE deployment descriptors.
– Each artifact type has associated with it builders, creation
wizards, syntax-aware editors, validators, semantic search
extensions, and refactoring support.
– Editors provide first-class programmer assistance such as code
completion, syntax coloring, error markers, and quick fixes.
Eclipse Web Tools Platform
• Key design goal of WTP - all of the functions that Eclipse users
have come to expect from Java source code will "just work" for
the new artifacts.
– For example, if I select a JSP, I can Run or Debug it.
– Similarly, the Debug command will run my J2EE server in
debug mode and the standard Eclipse Debugger will let me
step through my JSP source code.
– My JSP editor will provide code completion for both JSP tags
and inlined Java scriptlets.
Geronimo Eclipse Plug-in
• Extends WTP’s server tools to add Apache Geronimo
as a new server type.
• Provides form based editors for Geronimo Deployment
Plans.
Eclipse WTP without Geronimo Eclipse Plug-in
Eclipse WTP with Geronimo Eclipse Plug-in
Agenda
• Apache Geronimo - An Introduction
• Eclipse & Web Tools Platform
– Eclipse - Introduction, Java Development Tools
– Web Tools Platform, Geronimo Eclipse Plug-in
• Hands-on Exercises - 1
–
–
–
–
–
–
–
IDE Setup
Start Geronimo server
Create a new Web application with a single JSP
Deploy it on to Server
Make changes to the Web application and Redeploy
Undeploy Web application
Stop Geronimo Server
IDE Setup
1. Download & Install Geronimo v1.1.1 from http://geronimo.apache.org/downloads.html
–
“Geronimo 1.1.1 with Tomcat (zip)” is used in this presentation.
2. Download and install WTP v1.5.2 all-in-one-sdk from
http://download.eclipse.org/webtools/downloads/drops/R1.5/R-1.5.2-200610261841/
–
–
This includes Eclipse v3.2.1, WTP v1.5.2 and WTP’s pre-requisites namely EMF, GEF
& JEM.
“wtp-all-in-one-sdk-R-1.5.2-200610261841-win32.zip” is used in this presentation.
3. Download and install Geronimo Eclipse Plug-in v1.2 as per the instructions at
http://geronimo.apache.org/development-tools.html
4. Download and install Geronimo Eclipse Plug-in v1.2 as per the instructions at
http://xdoclet.sourceforge.net/xdoclet/install.html
–
XDoclet v1.2.3 is used in this presentation.
Finally start eclipse with “-clean” option (i.e. “eclipse -clean”) and switch to “J2EE”
perspective (Window -> Open Perspective -> Other -> J2EE -> OK).
Add Geronimo runtime into WTP
1.
Go to “Window -> Preferences -> Server -> Installed Runtimes”.
2.
Click on “Add” to add the installed Geronimo instance into the
WTP environment.
3.
In the “New Server Runtime” wizard, select “Apache -> Apache
Geronimo v1.1” and click “Next”.
4.
Click “Browse” and specify the root directory of Apache
Geronimo installation.
5.
Click “Finish” to complete adding new server runtime. Click
“OK” to close “Preferences” dialog.
Add Geronimo runtime into WTP
Step 3
Step 4
Add Geronimo runtime into WTP
6.
Click on the “Servers” tab (bottom half of Eclipse window).
7.
In the “Servers” view, right click and select “New->Server” from
the context menu
8.
“Apache Geronimo v1.1 Server” should already be selected as
the “server type” and the server runtime that you just created
should also be selected under “Server runtime”. Click Finish.
Start Geronimo server
1.
2.
In the “Servers” view select the server you just configured, right
click and select “Start” from the context menu.
You will be switched to the “Console” view while the server is
starting. Verify that you are switched back to the “Servers” view
and that Status of your server now says “Started” as shown below:
Create a new Web application
1.
From the Eclipse menu select “File>New->Other”
2.
Expand “Web” and select “Dynamic
Web Project” as shown in fig. Click
“Next”.
3.
Enter “SimpleWeb” as the project name and make sure “Apache
Geronimo v1.1” is selected as the “Target runtime”.
4.
Click “Next”. Make sure the “Geronimo Deployment” Project
Facet is selected.
5.
Click “Next”. Accept the default web module settings and click
“Finish”. (Note if you get a warning about caching a resource
copyrighted by Sun Microsystems click on “I Agree”).
Create a simple JSP
1.
From the “Project Explorer” view
expand the “SimpleWeb” project and
then select the “WebContent” folder.
Right click and select “New->JSP”
from the context menu as shown
2.
Name the JSP “index.jsp” and click “Finish”.
3.
This brings up the JSP editor. Enter the text “Hello World”
between the <body> </body> HTML tags.
4.
Type “Ctrl+S” to save the changes. Leave the editor window open
Deploy the Web application
1. From the Eclipse menu select “Window->Web Browser” and then
select either “Internet Explorer” or “Firefox”. Without this step
Eclipse will use its Internal browser which takes up a lot of valuable
screen space within your Eclipse IDE.
2. From the “Project Explorer” view expand “SimpleWeb” and
“WebContent”.
3. Select “index.jsp” , right click and select “Run As -> Run on Server”
4. Accept the defaults in the “Run on Server” dialog and click “Finish”.
5. A browser window should appear with the contents of “index.jsp”.
Leave the browser window open.
Make changes to the Web
application and redeploy
1. Go back to the JSP editor and change “Hello World” to
something else (e.g. Hello Underworld).
2. Type Ctrl+S to save the changes.
3. Go to the “Servers” view and select your server. Note that the
State column now says “Republish”. Right click and select
“Publish” from the context menu. Verify that the State of the
server changes to “Synchronized”.
4. Go back to the browser window that you used to test the JSP
initially and reload the page. Verify that the changes you made to
the JSP are reflected in the browser.
Undeploy web application
1.
Go to the “Servers” view and select your server. Right click and
select “Add and Remove Projects” from the context menu
2.
In the resulting dialog click on “Remove all” and then click on
“Finish”
Stop Geronimo server
1.
Go back to the “Servers” view and select your server. Right click
and select “Stop” from the context menu.
Agenda
•
Hands-on Exercises - 2: Develop a simple J2EE application
– Application Overview & Design
– Create & populate Database Tables using Admin Console.
– Create EJB project
• Create Container Managed Entity Beans using XDoclet Wizard
• XDoclet - Introduction
• Create Stateless Session Bean
– Create Web project
• Create Servlet & JSPs
– Create Enterprise Application Project and add EJB & Web projects into it.
– Create Geronimo Deployment Plan
– Deploy & Run the J2EE application
•
Hands-on Exercises - 3: Debug the J2EE application
•
Web Service Tools
Application Overview &
Design
EJB sample application from Apache Geronimo v1.1 Samples
http://cwiki.apache.org/GMOxDOC11/ejb-sample-application.html
Banking Application – Overall Architecture
Banking Application – Database Design
Database
Embedded
Apache Derby
Banking Application - Implementation
customer_info.jsp
index.jsp
CustomerServiceServlet
JSP &
Servlets
error.jsp
BankManagerFacadeBean
CustomerBean
AccountBean
Stateless
Session Bean
Container
Managed
Entity Beans
Agenda
•
Hands-on Exercises - 2: Develop a simple J2EE application
– Application Overview & Design
– Create & populate Database Tables using Admin Console.
– Create EJB project
• Create Container Managed Entity Beans using XDoclet Wizard
• XDoclet - Introduction
• Create Stateless Session Bean
– Create Web project
• Create Servlet & JSPs
– Create Enterprise Application Project and add EJB & Web projects into it.
– Create Geronimo Deployment Plan
– Deploy & Run the J2EE application
•
Hands-on Exercises - 3: Debug the J2EE application
•
Web Service Tools
Create & populate Database using
Admin Console
1.
Start Geronimo server as before.
2.
Open Admin Console, by right clicking on your server in Servers view
and select “Launch Geronimo Console”.
3.
In “Console Navigation” portlet of Admin Console, click on “DB
Manager” under “Embedded DB”.
4.
In “Create DB” textbox, type “BankDB” and click “Create”. This creates
a new Embedded Database by name “BankDB”. Make sure “Run SQL”
view displays the Result as “Database created: BankDB” (scroll down to
see Result).
5.
Select “BankDB” next to “Use DB”, then copy the following SQL
statements into the textbox below and click “Run SQL”. This creates the
required Tables in “BankDB” and populates them. Make sure “Run SQL”
view displays Result as “SQL command/s successful”.
Create & populate Database using
Admin Console
CREATE TABLE customer(
CUST_ID VARCHAR(15) PRIMARY KEY,
CUST_NAME VARCHAR(40) );
CREATE TABLE account(
ACC_NO VARCHAR(15) PRIMARY KEY,
ACC_TYPE VARCHAR(10),
BALANCE DOUBLE,
CUSTID_FK VARCHAR(15),
FOREIGN KEY (CUSTID_FK) REFERENCES customer);
INSERT INTO customer(CUST_ID,CUST_NAME)
VALUES('12345','Panini');
INSERT INTO account(ACC_NO,ACC_TYPE,BALANCE,CUSTID_FK)
VALUES('1234567890','Savings',1005.35,'12345');
INSERT INTO account(ACC_NO,ACC_TYPE,BALANCE,CUSTID_FK)
VALUES('2345678901','Current',999.95,'12345');
Agenda
•
Hands-on Exercises - 2: Develop a simple J2EE application
– Application Overview & Design
– Create & populate Database Tables using Admin Console.
– Create EJB project
• Create Container Managed Entity Beans using XDoclet Wizard
• XDoclet - Introduction
• Create Stateless Session Bean
– Create Web project
• Create Servlet & JSPs
– Create Enterprise Application Project and add EJB & Web projects into it.
– Create Geronimo Deployment Plan
– Deploy & Run the J2EE application
•
Hands-on Exercises - 3: Debug the J2EE application
•
Web Service Tools
Create EJB project
1. From the Eclipse menu select “File->New->Other”
2. Expand “EJB” and select “EJB Project”.
3. Enter “BankEJB” as the project name, select “Apache Geronimo
v1.1” as the “Target runtime” and select “EJB Project with
XDoclet” under “Configurations”. Click “Finish”.
Create Container Managed Entity
Beans using XDoclet Wizard
1.
In the “Project Explorer” view right click on “BankEJB” project and
select “New” -> “XDoclet Enterprise Java Bean”.
2.
Click on “Preferences”, select Version as “1.2.3”. Click “Browse” and
specify the path of XDoclet v1.2.3 Home. Click “OK”.
3.
In the “Create EnterpriseJavaBean” wizard select “Container Manager
Entity Bean” and click “Next”.
4.
Specify Java package as “org.apache.geronimo.samples.bank.ejb” and
Class name as “CustomerBean”. Click “Next”.
5.
Make sure “Import attributes from table” is selected as the “Usecase”.
Click “Next”.
Create Container Managed Entity
Beans using XDoclet Wizard
6.
In the “New JDBC Connection” page click on “New…”.
7.
In “Select a database manager” expand “Derby” and select “10.1”.
8.
Click “Browse” next to “Database location” and specify the path of
“BankDB” embedded database which will be located at “<Geronimo
Home>/var/derby/BankDB”.
9.
Click “Browse” next to “Class location” and specify the path of JAR that
implements the JDBC driver class which for our purpose will be
“<Geronimo Home>/repository/org/apache/derby/derby/10.1.1.0/derby10.1.1.0.jar”.
10.
Click “Finish”. Make sure no errors are thrown and “BankDB” appears in
“Available Connection Definitions”. Then click “Next”. If Geronimo is
already running, you might get a connection failed error. Stop Geronimo in
such cases.
Create Container Managed Entity
Beans using XDoclet Wizard
11.
Select “APP.CUSTOMER” as “Table” and click “Finish”. This would
create XDoclet based “CustomerBean.java”. XDoclet engine would be run
automatically to create stub classes for CustomerBean EJB and to update
“ejb-jar.xml”.
12.
Repeat the above steps and create another “XDoclet Enterprise JavaBean > Container Managed Entity Bean -> Java package:
org.apache.geronimo.samples.bank.ejb -> Class name: AccountBean ->
Usecase: Import attributes from table -> select BankDB -> Table:
APP.ACCOUNT -> click Finish”.
13.
In “Project Explorer”, expand “BankEJB -> ejbModule ->
org.apache.geronimo.samples.bank.ejb” and double click on
“CustomerBean.java” to open it in an editor.
14.
Add the following functions (in next page) inside “CustomerBean” class
say before “ejbActivate()” method. These methods let us query all the
Accounts held by a given Customer.
Create Container Managed Entity
Beans using XDoclet Wizard
/**
* @ejb.interface-method view-type="local"
* @ejb.relation name = "Customer-Account" target-ejb = "Account"
*
target-role-name = "Account-to-Customer"
*
role-name = "Customer-to-Account"
* @return
*/
public abstract Collection getAccounts();
/**
* @ejb.interface-method view-type="local"
* @param account
*/
public abstract void setAccounts(Collection accounts);
15.
Press “Ctrl + Shift + O” to organize the imports and press “Ctrl + S” to
save the changes and to run XDoclet engine to pick up code changes in
“CustomerBean.java” and update XDoclet generated stub classes and “ejbjar.xml”
Agenda
•
Hands-on Exercises - 2: Develop a simple J2EE application
– Application Overview & Design
– Create & populate Database Tables using Admin Console.
– Create EJB project
• Create Container Managed Entity Beans using XDoclet Wizard
• XDoclet - Introduction
• Create Stateless Session Bean
– Create Web project
• Create Servlet & JSPs
– Create Enterprise Application Project and add EJB & Web projects into it.
– Create Geronimo Deployment Plan
– Deploy & Run the J2EE application
•
Hands-on Exercises - 3: Debug the J2EE application
•
Web Service Tools
XDoclet
• Originated as a tool for creating EJBs, and has now
evolved into a general-purpose code generation engine.
• Just as you write JavaDoc tags for your code, you now
add XDoclet tags too.
• These tags are used by XDoclet as providers of metadata that is then used to generate other files such as
XML descriptors and/or source code from it.
• http://xdoclet.sourceforge.net/xdoclet/index.html
Sample XDoclet Tags
/**
* <!-- begin-xdoclet-definition -->
* @ejb.bean name="Customer"
*jndi-name="Customer"
*type="CMP"
* primkey-field="cust_id"
* schema="CustomerSCHEMA"
* cmp-version="2.x"
*
* @ejb.persistence
* table-name="APP.CUSTOMER"
*
* @ejb.finder
* query="SELECT OBJECT(a) FROM CustomerSCHEMA as a"
* signature="java.util.Collection findAll()"
*
* @ejb.pk class="java.lang.String"
*
* <!-- end-xdoclet-definition -->
**/
public abstract class CustomerBean implements javax.ejb.EntityBean {
Agenda
•
Hands-on Exercises - 2: Develop a simple J2EE application
– Application Overview & Design
– Create & populate Database Tables using Admin Console.
– Create EJB project
• Create Container Managed Entity Beans using XDoclet Wizard
• XDoclet - Introduction
• Create Stateless Session Bean
– Create Web project
• Create Servlet & JSPs
– Create Enterprise Application Project and add EJB & Web projects into it.
– Create Geronimo Deployment Plan
– Deploy & Run the J2EE application
•
Hands-on Exercises - 3: Debug the J2EE application
•
Web Service Tools
Create Session Bean using
XDoclet Wizard
1.
In the “Project Explorer” view right click on “BankEJB” project and select “New” > “XDoclet Enterprise Java Bean”.
2.
Select “Session Bean” and click “Next”.
3.
Specify Java package as “org.apache.geronimo.samples.bank.ejb” and Class name
as “BankManagerFacadeBean”. Click “Next”.
4.
Select “State Type” as “Stateless” and “Transaction Type” as “Container”. Click
“Finish”. “BankManagerFacadeBean.java” will now be created and XDoclet engine
will be run to generate EJB stub classes and to update “ejb-jar.xml”
5.
Double click on “BankManagerFacadeBean.java” to open it in an editor and add
the following Business methods (in next two pages) inside it, say after
“ejbCreate()” method.
Create Session Bean using
XDoclet Wizard
/**
*
* @ejb.interface-method view-type="local"
* @return
*/
public Collection getAccountInformation(String customerId){
Context context = null;
ArrayList accountBeanList = new ArrayList();
try {
context = new InitialContext();
CustomerLocalHome customerHome = (CustomerLocalHome) context.lookup(CustomerLocalHome.COMP_NAME);
CustomerLocal customer = customerHome.findByPrimaryKey(customerId);
Iterator accountIterator = customer.getAccounts().iterator();
while (accountIterator.hasNext()) {
AccountData account = new AccountData();
AccountLocal accountLocal = (AccountLocal) accountIterator.next();
account.setAcc_no(accountLocal.getAcc_no());
account.setBalance(accountLocal.getBalance());
account.setAcc_type(accountLocal.getAcc_type());
accountBeanList.add(account);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
context.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
return accountBeanList;
}
Create Session Bean using
XDoclet Wizard
/**
* @ejb.interface-method view-type="local"
* @param customerCode
* @return
*/
public CustomerLocal getCustomer(String customerCode) {
Context context = null;
CustomerLocal customer = null;
try {
context = new InitialContext();
CustomerLocalHome customerHome = (CustomerLocalHome)
context.lookup(CustomerLocalHome.COMP_NAME);
customer = customerHome.findByPrimaryKey(customerCode);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
context.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
return customer;
}
Create Session Bean using
XDoclet Wizard
6.
Press “Ctrl + Shift + O” to organize imports. Resolve any ambiguous names by
selecting the correct type to import.
7.
Add following XDoclet tag inside the XDoclet definition at the beginning of
BankManagerFacadeBean class, say before “ * <!-- end-xdoclet-definition -->”.
XDoclet will read this tag and add an <ejb-local-ref> element inside “ejb-jar.xml”.
This is to allow BankManagerFacadeBean Session Bean to access CustomerBean
EJB.
* @ejb.ejb-ref ejb-name="Customer" view-type="local" ref-name="ejb/CustomerLocal"
8.
Press “Ctrl + S” to save changes and for XDoclet to be run to update stub classes
and “ejb-jar.xml”.
Agenda
•
Hands-on Exercises - 2: Develop a simple J2EE application
– Application Overview & Design
– Create & populate Database Tables using Admin Console.
– Create EJB project
• Create Container Managed Entity Beans using XDoclet Wizard
• XDoclet - Introduction
• Create Stateless Session Bean
– Create Web project
• Create Servlet & JSPs
– Create Enterprise Application Project and add EJB & Web projects into it.
– Create Geronimo Deployment Plan
– Deploy & Run the J2EE application
•
Hands-on Exercises - 3: Debug the J2EE application
•
Web Service Tools
Create Web Project & Servlet
1.
Create a Dynamic Web project with name “BankWeb” as below: “File ->
New -> Other -> expand Web -> select Dynamic Web Project -> specify
Project name: BankWeb -> Finish”.
2.
In the “Project Explorer” view right click on “BankWeb” project and
select “New” -> “Servlet”.
3.
Specify Java package as “org.apache.geronimo.samples.bank.web” and
Class name as “CustomerServiceServlet”. Click “Next”.
4.
In “URL Mappings” select “/CustomerServiceServlet” and click “Edit”.
Change the URL mapping to “/customer_info”. (JSPs that we will copy
next are using this URL and hence this change). Click “OK”.
5.
Click “Finish”. “CustomerServiceServlet.java” will now be created and
elements for it will be added in “web.xml”.
6.
Update the doGet() method of CustomerServiceServlet as shown next:
Create Web Project & Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Context context = null;
String path = "/jsp/error.jsp";;
try {
String customerCode = request.getParameter("customerCode");
context = new InitialContext();
BankManagerFacadeLocalHome bmHome =
(BankManagerFacadeLocalHome)context.lookup(BankManagerFacadeLocalHome.COMP_NAME);
BankManagerFacadeLocal bm = bmHome.create();
Collection accountList = bm.getAccountInformation(customerCode);
CustomerLocal customer = bm.getCustomer(customerCode);
if(accountList != null && customer!= null){
request.setAttribute("accountList",accountList);
request.setAttribute("customerCode",customerCode);
request.setAttribute("customerName",customer.getCust_name());
path = "/jsp/customer_info.jsp";
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
context.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
getServletContext().getRequestDispatcher(path).forward(request,response);
}
Create Web Project & Servlet
7.
Forward call to doPost() method to doGet() as below:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
doGet(request, response);
}
8.
To resolve references to EJBs referred in doGet() method add “BankEJB”
project into the build path of “BankWeb” by “Right clicking on BankWeb
project -> Properties -> Java Build Path -> Projects -> Add -> select
BankEJB -> OK”.
9.
Press “Ctrl + Shift + O” to organize imports. Resolve any ambiguous
names by selecting the correct type to import.
Copy JSPs into Web Project
1.
In Project Explorer view, expand “BankWeb” and right click on “WebContent” folder
and click “Import”.
2.
Expand “General” and select “File System”. Click “Next”.
3.
Click “Browse” next to “From directory” and select the directory where you have
extracted “BankExample.zip”. Click “OK”.
4.
On the left side of “Import” wizard, select & check “jsp” folder.
5.
Make sure “Into folder” says “BankWeb/WebContent”.
6.
Click “Finish”. You will observe that a “jsp” folder is created in “WebContent”
directory of “BankWeb” project and following three files are copied inside of that
“jsp” folder: “index.jsp”, “customer_info.jsp” and “error.jsp”.
7.
Import from File System can also be done by dragging “jsp” folder from “Windows
Explorer” and dropping it on “WebContent” directory of “BankWeb” project.
Update “web.xml”
1.
In “Project Explorer” view expand “BankWeb” -> “WebContent” ->
“WEB-INF” and double click on “web.xml” to open it in an editor.
2.
Add the following elements inside <web-app> element say just before
</web-app>. This resolves JNDI lookup of BankManagerFacade EJB in
CustomerServiceServlet.
<!-- To refer local EJB's -->
<ejb-local-ref>
<ejb-ref-name>ejb/BankManagerFacadeLocal</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local-home>org.apache.geronimo.samples.bank.ejb.BankManagerFacadeLocalHome</local-home>
<local>org.apache.geronimo.samples.bank.ejb.BankManagerFacadeLocal</local>
<ejb-link>BankManagerFacade</ejb-link>
</ejb-local-ref>
3.
Update <welcome-file-list> element as below and press “Ctrl +S” to
save changes.
<welcome-file-list>
<welcome-file>jsp/index.jsp</welcome-file>
</welcome-file-list>
Agenda
•
Hands-on Exercises - 2: Develop a simple J2EE application
– Application Overview & Design
– Create & populate Database Tables using Admin Console.
– Create EJB project
• Create Container Managed Entity Beans using XDoclet Wizard
• XDoclet - Introduction
• Create Stateless Session Bean
– Create Web project
• Create Servlet & JSPs
– Create Enterprise Application Project and add EJB & Web projects into it.
– Create Geronimo Deployment Plan
– Deploy & Run the J2EE application
•
Hands-on Exercises - 3: Debug the J2EE application
•
Web Service Tools
Create Enterprise Application Project
1.
From Eclipse menu, select “File” -> “New” -> “Project”.
2.
Expand “J2EE” and select “Enterprise Application Project”. Click “Next”.
3.
Specify “Project name” as “Bank”. Click “Next”. Click “Next” again.
4.
In “J2EE Modules to Add to the EAR” page, select and check “BankEJB”
and “BankWeb” modules. Click “Finish”.
5.
In “Project Explorer” view expand “Bank” -> “EARContent” -> “METAINF” and double click on “application.xml” to open it in an editor.
6.
Change the <context-root> of web-module as below, to match with the
references used in JSPs. Press “Ctrl + S” to save changes.
<context-root>Bank</context-root>
Agenda
•
Hands-on Exercises - 2: Develop a simple J2EE application
– Application Overview & Design
– Create & populate Database Tables using Admin Console.
– Create EJB project
• Create Container Managed Entity Beans using XDoclet Wizard
• XDoclet - Introduction
• Create Stateless Session Bean
– Create Web project
• Create Servlet & JSPs
– Create Enterprise Application Project and add EJB & Web projects into it.
– Create Geronimo Deployment Plan
– Deploy & Run the J2EE application
•
Hands-on Exercises - 3: Debug the J2EE application
•
Web Service Tools
Create Geronimo Deployment Plan
1.
In “Project Explorer” view expand “Bank” -> “EARContent” -> “METAINF” and double click on “geronimo-application.xml” to open it in an
editor.
2.
Replace the contents of this file with the contents provided in “geronimoapplication.xml” file of “BankExample.zip”.
You will notice that this deployment plan specifies the mappings between Entity
Beans and database tables/columns. It also defines a database pool by name
“BankPool” and Entity Beans are defined to use this database pool.
Please note that the Form Based Editors for Geronimo Deployment Plans provided
by Geronimo Eclipse Plug-in is currently limited in its functionality and as
such Geronimo Deployment Plans have to be created manually.
You can refer Aaron Mulder’s book on “Apache Geronimo Development and
Deployment” for detailed information on creating Geronimo Deployment
Plans.
Agenda
•
Hands-on Exercises - 2: Develop a simple J2EE application
– Application Overview & Design
– Create & populate Database Tables using Admin Console.
– Create EJB project
• Create Container Managed Entity Beans using XDoclet Wizard
• XDoclet - Introduction
• Create Stateless Session Bean
– Create Web project
• Create Servlet & JSPs
– Create Enterprise Application Project and add EJB & Web projects into it.
– Create Geronimo Deployment Plan
– Deploy & Run the J2EE application
•
Hands-on Exercises - 3: Debug the J2EE application
•
Web Service Tools
Deploy and Run the J2EE App
1.
Start Geronimo if it is not already started.
2.
Go to “Servers” view. Right click on your server and click “Add and
Remove Projects…”.
3.
Select “Bank” under “Available projects:” and click “Add >”. Click
“Finish”. The EAR will now be deployed onto server. Wait until server
“State” is shown as “Synchronised” (You might have to switch back to the
“Servers” view to see the server “State”).
4.
Open http://localhost:8080/Bank in a Browser.
5.
Enter “Customer Id:” as “12345” and press “View”. Customer Account
Information will now be displayed.
6.
Click “Home”. Enter “Customer Id:” as “6789” and press “View”. An error
message will be thrown as this Customer doesn’t exist in database.
Agenda
•
Hands-on Exercises - 2: Develop a simple J2EE application
– Application Overview & Design
– Create & populate Database Tables using Admin Console.
– Create EJB project
• Create Container Managed Entity Beans using XDoclet Wizard
• XDoclet - Introduction
• Create Stateless Session Bean
– Create Web project
• Create Servlet & JSPs
– Create Enterprise Application Project and add EJB & Web projects into it.
– Create Geronimo Deployment Plan
– Deploy & Run the J2EE application
•
Hands-on Exercises - 3: Debug the J2EE application
•
Web Service Tools
Debug the J2EE application
1.
In “Project Explorer” view expand “BankWeb” -> “Java Resources: src” ->
“org.apache.geronimo.samples.bank.web” and double click on
“CustomerServiceServlet.java” to open it in an editor.
2.
In the left bar of Editor window, double click on some java code (say the first
executable line of doGet() method). This will create a line break point at that code.
3.
Go back to “Servers” view, right click on your server and select either “Debug” (if
your server is not already started) or “Restart -> Debug” (if server is already started).
This will start your server in Debug mode.
4.
Open http://localhost:8080/Bank in a Browser. Enter “Customer Id:” as “6789” and
press “View”.
5.
Eclipse window will pop up asking for Perspective Switch to Debug perspective.
Click Yes. Eclipse will now look as shown in next page.
Debug the J2EE application
6.
You can now step through our Servlet code, viewing and changing variable values
as you go.
7.
Select following line, right click and select “Run to Line”. You will see that code
is run up to that line.
“String customerCode = request.getParameter("customerCode");
8.
Click “Step Over” icon in the “Debug view” to execute the above line
9.
In the “Variables” view, select “customerCode” variable and click in the “Value”
column. You will see that the value of “customerCode” can now be changed.
Change the value number from “6789” to “12345”. Click “Enter”
10.
Back in the “Debug” view, click the green icon to continue execution.
11.
In the browser you will observe that Account Information for “Customer Id:
12345” is displayed.
Agenda
•
Hands-on Exercises - 2: Develop a simple J2EE application
– Application Overview & Design
– Create & populate Database Tables using Admin Console.
– Create EJB project
• Create Container Managed Entity Beans using XDoclet Wizard
• XDoclet - Introduction
• Create Stateless Session Bean
– Create Web project
• Create Servlet & JSPs
– Create Enterprise Application Project and add EJB & Web projects into it.
– Create Geronimo Deployment Plan
– Deploy & Run the J2EE application
•
Hands-on Exercises - 3: Debug the J2EE application
•
Web Service Tools
Eclipse WTP - Web Service tools
• WSDL editor - both a source editor and a graphical editor.
• Web Service Explorer that lets you search and publish to UDDI
registries, and dynamically test WSDL-based Web Services.
• Web Service Wizard that ties together the full development
lifecycle. It lets you deploy Java classes as Web Services,
generate server and client code from WSDL, and generate test
clients, as well as being integrated with the Web Service Explorer
for publishing and discovery.
• Web Service Interoperability (WS-I) Test Tools that lets you
validate WSDL and SOAP for compliance with the WS-I profiles.
• TCP/IP monitor that's very handy for debugging HTTP traffic.
Q&A
Shiva Kumar H.R.
[email protected]
References
•
IBM developerWorks article by author Sing Li – “Geronimo! Part 1: The
J2EE 1.4 engine that could”
–
•
Arthur Ryman’s article “Eclipse: The Story of Web Tools Platform 0.7” at
Java Developer's Journal
–
•
http://www.ddj.com/dept/java/184406194
IBM GetStarted with Application Deployment on WebSphere Application
Server Community Edition
–
•
http://java.sys-con.com/read/111212.htm
Eric J. Bruno’s article “NetBeans 4.1 & Eclipse 3.1” at Dr. Dobb's Portal
–
•
http://www-128.ibm.com/developerworks/java/library/j-geron1/index.html
http://www-306.ibm.com/software/webservers/appserv/community/services/
Apache Geronimo v1.1 EJB sample application
–
http://cwiki.apache.org/GMOxDOC11/ejb-sample-application.html