I am trying for the first time to get a servlet to find a bean in Jonas.
After creating and deploying a war file to Tomcat, the server can find my
servlet but I get the following error
Cannot lookup TestHome: java.lang.NullPointerException
This occurs in my servlet on the following line of code:
home =
(TestHome)PortableRemoteObject.narrow(initialContext.lookup("TestHome"),
TestHome.class);
When running a standalone client, I can find the same bean and get expected
results from the methods in the bean. What must I do for the servlet to
'see' the bean in the EJBServer (is the servlet finding the server) ?
I am running NT using Jonas 2.2.7 and Tomcat 4. The sb and eb samples worked
fine. I have not been able to get the security sample to compile correctly
in cygwin thus the attempt to create something on my own.
Here is most of the java code and xml files that I am using. Please let me
know what I am missing. Thanks.
my servlet
********** HelloEJB.java****************
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.rmi.PortableRemoteObject;
import test.Test;
import test.TestHome;
public class HelloEJB extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<body>");
out.println("<ul>") ;
Context initialContext = null;
try {
initialContext = new InitialContext();
} catch (Exception e) {
out.println("<li>Cannot get initial context for JNDI: " + e +
"</li>");
return;
}
// Connecting to TestHome thru JNDI
TestHome home = null;
try {
home =
(TestHome)PortableRemoteObject.narrow(initialContext.lookup("TestHome"),
TestHome.class);
} catch (Exception e) {
out.println( "<li>Cannot lookup TestHome: " + e + "</li>");
return;
}
// TestBean creation
Test t1 = null;
try {
System.out.println("Create a bean");
t1 = home.create("User1");
} catch (Exception e) {
System.err.println("Cannot create TestBean: " + e);
System.exit(2);
}
// First transaction (committed)
try {
String s = "Tag this...";
String s2 = t1.TD_tag(s);
System.out.println(s2);
} catch (Exception e) {
System.err.println("exception during 1st Tx: " + e);
System.exit(2);
}
out.println("</ul><br />") ;
out.println("Hello World");
out.println("</body>");
out.println("</html>");
}
}
*********** end of servlet ************
*********** start Test class **********
package test;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface Test extends EJBObject {
public void buy (int Shares) throws RemoteException;
public int read () throws RemoteException;
public String TD_tag(String s) throws RemoteException;
}
********* end of Test class ***********
********* start of TestHome class *****
package test;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface TestHome extends EJBHome {
Test create(String user) throws CreateException, RemoteException;
}
******** end of TestHome class ********
******* start of TestBean class *******
package test;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.EJBObject;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.SessionSynchronization;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class TestBean implements SessionBean, SessionSynchronization {
protected String clientUser = null;
protected SessionContext sessionContext = null;
public void ejbCreate(String user) {
clientUser = user;
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void ejbRemove() {
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public void afterBegin() {
}
public void beforeCompletion() {
try {
InitialContext ictx = new InitialContext();
String value = (String) ictx.lookup("java:comp/env/prop1");
// value should be the one defined in ejb-jar.xml
} catch (NamingException e) {
throw new EJBException(e);
}
}
public void afterCompletion(boolean committed) {
}
public String TD_tag(String s) {
return "<TD>" + s + "</TD>";
}
}
********** end of TestBean class ************
********** start ejb-jar.xml ****************
<!DOCTYPE ejb-jar SYSTEM "../../../../xml/ejb-jar_1_1.dtd">
<ejb-jar>
<description>Deployment descriptor for the test JOnAS
example</description>
<enterprise-beans>
<session>
<ejb-name>Test</ejb-name>
<home>test.TestHome</home>
<remote>test.Test</remote>
<ejb-class>test.TestBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
<env-entry>
<env-entry-name>prop1</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>prop1 value</env-entry-value>
</env-entry>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Test</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
************ end of ejb-jar.xml ****************
************ start of jonas-ejb-jar.xml ******
<!DOCTYPE jonas-ejb-jar SYSTEM "../../../../xml/jonas-ejb-jar.dtd">
<jonas-ejb-jar>
<jonas-session>
<ejb-name>Test</ejb-name>
<jndi-name>TestHome</jndi-name>
</jonas-session>
</jonas-ejb-jar>
************ end of jonas-ejb-jar.xml ******
************ start web.xml *****************
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>HelloEJB</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
test
</servlet-name>
<url-pattern>
/secured/test
</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<!-- Define the context-relative URL(s) to be protected -->
<url-pattern>/secured/*</url-pattern>
<!-- If you list http methods, only those methods are protected -->
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<!-- Anyone with one of the listed roles may access this area -->
<role-name>tomcat</role-name>
<role-name>role1</role-name>
</auth-constraint>
</security-constraint>
<!-- Default login configuration uses BASIC authentication -->
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Example Basic Authentication Area</realm-name>
</login-config>
</web-app>
************* end of web.xml *********************
----
To unsubscribe, send email to [EMAIL PROTECTED] and
include in the body of the message "unsubscribe jonas-users".
For general help, send email to [EMAIL PROTECTED] and
include in the body of the message "help".