Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!

sid ansari

Greenhorn
+ Follow
since Nov 15, 2005
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by sid ansari

Hi Chris,

Thanks for your help. I guess I didn't make myself clear when I asked for help on deploying this application. I know how to deploy web applications and beans. My problem is that when I put the local interfaces and bean in a jar, and the servlet and html in a war and then deploy them in an application, I get a Null Pointer error. Could you try them and see if you get the same error. I suspect it has to do with the fact that the servlet cannot get a reference to the EJBLocalObject.

Thanks again for your help

Sid
Hi Chris,

I have placed AdviceLocal, AdviceHomeLocal and the bean in one jar file. I have also written a servlet and html as follows:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.rmi.*;
import javax.ejb.*;


public class AdviceServlet extends HttpServlet {

AdviceLocal advisor = null;
Object objref = null;

public void init(ServletConfig config) throws ServletException {

try {

InitialContext ic = new InitialContext();
Object objref = ic.lookup("java:comp/env/ejb/Advisor");

} catch (NamingException ex) {
ex.printStackTrace();
}
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

AdviceHomeLocal home = (AdviceHomeLocal)objref;

try {

advisor = home.create();

} catch (CreateException ex) {

ex.printStackTrace();

}

System.out.println(advisor.getAdvice());


}


public void destroy() {

System.out.println("AdviceServlet: destroy()");
}
}


the html is:

<html>
<head>
<title>Advice</title>
</head>

<body bgcolor="white">
<h1><b><center>Converter</center></b></h1>
<hr>
<p>Click on Button for Advise:</p>
<form action="http://localhost:8080/advice/stateful"; method="get">
<br>
<p>
<input type="submit" value="Submit">
</form>

</body>
</html>

How do I deploy these two. How do I make sure that the servlet is in the same VM as the beans and the interfaces. I have the app server on my laptop.

Thanks in advance if you can help.

Sid
Hi Chris,

Thanks for your help. I will try out what you suggest.

Thanks again

Sid
Good idea mark. Please shut this thread down else the irrationality might go on forever!

Sid
14 years ago
Hi Rene,

So let me see if I understand what you are saying. You are saying that I cannot use a stand alone local client running in the same VM as the bean with local interfaces to reference the bean

I think that if this is true then the authors of Head First EJB should take the example that I have posted out of their book because it is extremely misleading. How do I contact the authors?

Sid
Chris,

You are telling me how to deploy with remote interfaces. I know how to do that. I was wondering if someone could show me how to deploy local interfaces with a local client. I don't see why I have to do a PortableRemoteObject... lookup with a local client. I am not using IIOP so I don't see why I have to do that. I just need to know how to get the local client to reference the bean when I have local interfaces.

The question arises: can I even do that or is there a serious mistake in the Head First EJB book, because the example is taken staright from the book. How do I contact the authors?

Regards

Sid
Hi,

I am following the examples in the Head First EJB book regarding local interfaces. I have the Local Home interface, the Local interface, the Bean, and the Local Client. My problem is that I can only deploy this application if I put all four files in the same jar. Otherwise, the application will not deploy. And if the four files are in the same jar, then the question arises "How do I run the client?" What should I do? I am listing the files, and the app server that I am using is Sun's App Server 8 Q2:

import javax.ejb.*;
import java.rmi.RemoteException;

public interface AdviceLocal extends EJBLocalObject {

public String getAdvice();

}

import javax.ejb.*;
import java.rmi.RemoteException;

public interface AdviceHomeLocal extends EJBLocalHome {

public AdviceLocal create() throws CreateException;

}

import javax.ejb.*;

public class AdviceBean implements SessionBean {

private String[] adviceStrings = {"Read a Book","Listen to some music"};

public void ejbActivate(){}

public void ejbPassivate(){}

public void ejbRemove(){}

public void setSessionContext(SessionContext ctx){}

public void ejbCreate(){}

public String getAdvice() {

System.out.println("Ready for advice?");
int random = (int) (Math.random() * adviceStrings.length);
return adviceStrings[random];

}
}

import javax.naming.*;
import javax.ejb.*;

public class AdviceLocalClient {

public static void main(String[] args) {

new AdviceLocalClient().go();

}

public void go() {

Object objref = null;

try {

Context initial = new InitialContext();
objref = initial.lookup("java:comp/env/ejb/Advisor");

} catch (NamingException ex) {

ex.printStackTrace();

}

AdviceHomeLocal home = (AdviceHomeLocal) objref;
AdviceLocal advisor = null;

try {

advisor = home.create();

} catch (CreateException ex) {

ex.printStackTrace();

}

System.out.println(advisor.getAdvice());


}
}