THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
package org.apache.geronimo.samples.myphonebookpak; import javax.ejb.Stateless; import javax.persistence.PersistenceUnit; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import org.apache.geronimo.samples.myphonebookpak.PhoneBook; @Stateless public class MyPhonebookBean implements MyPhonebookLocal { @PersistenceUnit(unitName="PhonePU") protected EntityManagerFactory emf; public MyPhonebookBean() { } public PhoneBook findByPrimaryKey(String name) { EntityManager em = emf.createEntityManager(); PhoneBook phonebook = (PhoneBook)em.find(PhoneBook.class, name); em.close(); return phonebook; } } |
index.jsp is the JSP page that uses the EJB to access the database.
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=Mattias">Mattias</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 just specifies the module's information.
...