Wiki Markup |
---|
{scrollbar} |
The Phone Book Bean Example
This is an example of a JSP-page calling an Enity Bean that uses annotations. The result looks like this:
Application Contents
First, let us take a look at the PhoneBook Entity Bean that represents a table in the database. Each instance of PhoneBook is a record of the table.
PhoneBook.java uses the
...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<%@ page contentType="text/html" import="org.apache.geronimo.samples.myphonebookpak.*, javax.naming.* " %> <% String searchName = ""; if (request.getParameter("searchname") != null) { searchName=request.getParameter("searchname"); } %> <html><head><title>Phonebook</title></head><body> <form action="index.jsp"> <b>Search number</b>:<br> Enter name: <input type="text" name="searchname" value="<%=searchName%>"> <input type="submit" value="Search"> (Test with <a href="index.jsp?searchname=Joe">Joe</a>) </form> <% if (! searchName.equals("")) { String number=""; try { Context context = new InitialContext(); MyPhonebookLocal myPhonebookLocal = (MyPhonebookLocal)context.lookup("java:comp/env/ejb/MyPhonebookBean"); PhoneBook phonebook = myPhonebookLocal.findByPrimaryKey(searchName); if(phonebook != null) { number = phonebook.getNumber(); } } catch (Exception e) { number=e.toString(); } out.println("This is the number returned from the EJB when searching for '"+searchName+"' : " + number); } %> </body></html> |
Deployment Plans for EJB
openejb-jar.xml specifies the module's information such as the group and artifact IDs and the verion and module type.
...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> <persistence-unit name="PhonePU"> <description>Phone Book</description> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <class>org.apache.geronimo.samples.myphonebookpak.PhoneBook</class> <properties> <property name="openjpa.ConnectionURL" value="jdbc:derby:PhoneBookDB" /> <property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.EmbeddedDriver" /> <property name="ConnectionUserName" value="app" /> <property name="openjpa.jdbc.SynchronizeMappings" value="false" /> </properties> </persistence-unit> <!-- <jta-data-source>PhoneBookPool</jta-data-source> <non-jta-data-source>PhoneBookPool</non-jta-data-source> --> </persistence> |
Deployment Plans for the Web-App
web.xml references the EJB by specifying the package to which the MyPhonebookLocal belongs to.
...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1" xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1" xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1" xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.1"> <sys:environment> <sys:moduleId> <sys:groupId>${pom.groupId}</sys:groupId> <sys:artifactId>${pom.artifactId}</sys:artifactId> <sys:version>${version}</sys:version> <sys:type>war</sys:type> </sys:moduleId> </sys:environment> <context-root>/myphonebook</context-root> </web-app> |
Deployment Plan for the Application
geronimo-application.xml tells the application that there is a database pool that needs to be deployed as well. The db pool is defined in PhoneBookPool.xml and the driver that is needs in order to be deployed is the tranql-connector-ra-1.3.rar file--these two files will reside on the top level layer of the resultant EAR file.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<?xml version="1.0" encoding="UTF-8"?> <application xmlns="http://geronimo.apache.org/xml/ns/j2ee/application-1.1" xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1" xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.1" application-name="t6"> <sys:environment> <sys:moduleId> <sys:groupId>${pom.groupId}</sys:groupId> <sys:artifactId>${pom.artifactId}</sys:artifactId> <sys:version>${version}</sys:version> <sys:type>ear</sys:type> </sys:moduleId> </sys:environment> <module> <connector>tranql-connector-ra-1.3.rar</connector> <alt-dd>PhoneBookPool.xml</alt-dd> </module> </application> |
Configuring, Building, and Deploying the Application
Download the MyPhoneBook application from the following link:
MyPhoneBook
After decompressing the given file, the myphonebook directory will be created.
Source Code
You can checkout the source code of this sample from SVN:
svn checkout http://svn.apache.org/repos/asf/geronimo/samples/trunk/samples/myphonebook
Creating and Populating Database
After starting Apache Geronimo log into the console and follow the given steps to create the PhoneBookDB.
...
- Select DB Manager link from the Console Navigation in the left.
- Give the database name as PhoneBookDB and click Create button.
- Select PhoneBookDB to the Use DB field.
- Open PhoneBookDB.sql in the myphonebook/myphonebook-ear/src/main/resources directory from a text editor.
- Paste the content PhoneBookDB.sql to the SQL Commands text area and press Run SQL button.
Building
Use a command prompt to navigate into the myphonebook directory and just give mvn install followed by mvn site command to build. It will create the myphonebook-ear-2.0-SNAPSHOT.ear under the myphonebook folder. Now, you are ready to deploy myphonebook application in the Geronimo Application server.
Deploying the Application
Deploying sample application is pretty straight forward as we are going to use the Geronimo Console.
- Scroll down to Deploy New from the Console Navigation panel.
- Load myphonebook-ear-2.0-SNAPSHOT.ear from myphonebook folder in to the Archive input box.
- Press Install button to deploy application in the server.
MyPhoneBook Web Application
To test the sample web application open a browser and type http://localhost:8080/myphonebook.
Untested Instructions for using jta-datasource in persistence.xml
This app does not use openjpa sequences so apparently you can get by with only a jta-datasource. In my experience apps that do use openjpa sequences to supply primary key values also need a non-jta-datasource. When deploying such a non-jta-datasource check the plan and make sure it says <no-transaction/> rather than <local-transaction/> or <xa-transaction/>.
...