You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jimmy Ray <ji...@yahoo.com> on 2005/06/07 20:23:49 UTC
OpenEJB JNDI issue -
I am trying to use openEJB 0.9.2 with Tomcat 5.0.28 on
W2K with the Sun 1.4.2 JDK.
I can
test everything OK with the call to
"http://localhost:8080/openejb_loader-0.9.2/". I can
even lookup my bean. Now when I try the JSP or
servlet I seem to be getting a JNDI NamingException,
"Naming Exception - Name "ejb/hello" not found".
In the past, to get JNDI working for DBCP, I have had
to have a servlet.xml, web.xml and <app_nam>.xml file
entry for this JNDI resource. Below is the code I am
trying.
Regards,
Jimmy Ray
THE JSP:
<%@ page import="org.acme.HelloObject,
org.acme.HelloHome,
javax.naming.InitialContext,
javax.naming.Context"%>
<html>
<head>
<title>OpenEJB -- EJB for Tomcat</title>
</head>
<body>
<%
Context initCtx = new InitialContext();
Object object =
initCtx.lookup("java:comp/env/ejb/hello");
if (object != null) {
HelloHome helloHome = (HelloHome)
javax.rmi.PortableRemoteObject.narrow(object,
HelloHome.class);
HelloObject bean = helloHome.create();
out.println(bean.sayHello());
}
%>
</body>
</html>
THE SERVER.XML ENTRY:
<Context docBase="${catalina.home}/webapps/EJB"
path="/EJB">
<Ejb name="ejb/hello"
type="Session"
home="org.acme.HelloHome"
remote="org.acme.Hello"/>
<ResourceParams name="ejb/hello">
<parameter>
<name>factory</name>
<value>org.openejb.client.TomcatEjbFactory</value>
</parameter>
<parameter>
<name>openejb.naming.factory.initial</name>
<value>org.openejb.client.LocalInitialContextFactory</value>
</parameter>
<parameter>
<name>openejb.naming.security.principal</name>
<value>Admin</value>
</parameter>
<parameter>
<name>openejb.naming.security.credentials</name>
<value>pass</value>
</parameter>
<parameter>
<name>openejb.naming.provider.url</name>
<value>localhost:4201</value>
</parameter>
<parameter>
<name>openejb.ejb-link</name>
<value>Hello</value>
</parameter>
</ResourceParams>
</Context>
The web.xml entry for the EJB webapp:
<ejb-ref>
<description>
EJB Reference to the bean deployed to
OpenEJB
</description>
<ejb-ref-name>ejb/hello</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<home>org.acme.HelloHome</home>
<remote>org.acme.Hello</remote>
</ejb-ref>
The application xml entry in the EJB.xml in
$CATALINA_HOME/conf/catalina/localhost:
<?xml version='1.0' encoding='utf-8'?>
<Context crossContext="true" debug="9"
displayName="EJB" docBase="EJB" path="/EJB"
workDir="c:/tomcat-5.0.28/work/Catalina/localhost/EJB">
<Logger
className="org.apache.catalina.logger.FileLogger"
directory="c:/tomcat-5.0.28/webapps/EJB/logs"
prefix="EJB_log." suffix=".txt" timestamp="true"
verbosity="4"/>
<ResourceLink global="ejb/hello" name="ejb/hello"
type="org.acme.Hello"/>
</Context>
THE SERVLET:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.acme.HelloHome;
import org.acme.HelloObject;
/*
* Created on Jun 7, 2005
*/
/**
* @author rayj
* @version 1.0
*/
public class TestHelloEJBServlet extends HttpServlet {
/** Processes requests for both HTTP
<code>GET</code> and <code>POST</code> methods.
* All the real work happens here.
* @param request servlet request
* @param response servlet response
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
protected void processRequest(HttpServletRequest
req,
HttpServletResponse res) throws
ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet</title>");
out.println("</head>");
out.println("<body>");
Properties p = new Properties();
//The JNDI properties you set depend
//on which server you are using.
//These properties are for the Remote Server.
/*p.put("java.naming.factory.initial",
"org.openejb.client.RemoteInitialContextFactory");
p.put("java.naming.provider.url",
"127.0.0.1:4201");
p.put("java.naming.security.principal",
"Admin");
p.put("java.naming.security.credentials",
"pass");*/
p.put("java.naming.factory.initial",
"org.openejb.client.LocalInitialContextFactory");
p.put("openejb.home",
"C:\\Programs\\openejb-0.9.2");
try {
// Now use those properties to create
//a JNDI InitialContext with the server.
InitialContext ctx = new
InitialContext(p);
// Lookup the bean using it's deployment id
Object obj = ctx.lookup("ejb/hello");
// Be good and use RMI remote object
narrowing
//as required by the EJB specification.
HelloHome ejbHome = (HelloHome)
PortableRemoteObject.narrow(obj,
HelloHome.class);
//Use the HelloHome to create a
HelloObject
HelloObject ejbObject = ejbHome.create();
//The part we've all been wainting for...
String message = ejbObject.sayHello();
//A drum roll please.
System.out.println(message);
out.println(message);
} catch (NamingException ne) {
out.println("Naming Exception -
"+ne.getMessage());
} catch (Exception e) {
out.println("Exception -
"+e.getMessage());
} finally {
out.println("</body>");
out.println("</html>");
out.close();
}
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string
in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag
value method equals to get.
*
* @param request the request send by the client
to the server
* @param response the response send by the server
to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag
value method equals to post.
*
* @param request the request send by the client
to the server
* @param response the response send by the server
to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init(ServletConfig config) throws
ServletException {
super.init(config);
}
}
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org