-
1. lookup a session bean inside a servlet
wdfink May 2, 2011 1:56 PM (in response to bertoz84)The standard JNDI binding starts with the name of the application jar/ear file.
E.g. BookShop is inside the book.ear the lookup is "book/BookShop/OperazionoUtenti/remote".
You might see the naming if you use the jmx-console and call the jndi-view.
But why do you use a remote access if you are within the same server?
-
2. lookup a session bean inside a servlet
bertoz84 May 2, 2011 3:15 PM (in response to wdfink)I knew that, infact i should use ( as i do ) BookShop/OperazioniUtenti/remote ( BookShop is a .jar inside my web project, i don't have yet a .war nor a .ear ). I don't get errors from the compiler but only at run-time.
...and i use the remote access because....i'm totally new with all jboss, java, ejb, jsp, servlet and i totally copying from tutorial and similar projects of my friends...so i'm not sure of what i do and if i do it the right way...
-
3. lookup a session bean inside a servlet
andi-g May 2, 2011 4:57 PM (in response to bertoz84)The fastest way to create a web project with ejb in Eclipse is using a Enterprise Application Project:
First u create Enterprise Application Project after that u create an EJB project where u put yours ejb components. In the end u create Dynamic Web Project with yours servlet.
-
4. lookup a session bean inside a servlet
bertoz84 May 3, 2011 2:24 AM (in response to andi-g)I created my BookShop.jar as an EJB Project; now i'm working on the web part ( JSP + servlet ). In the end i will create an Enterprise Project that include both my ejb and web projects to create the final .ear.
But now i need to use my session beans inside servlets...
-
5. lookup a session bean inside a servlet
andi-g May 3, 2011 8:53 AM (in response to bertoz84)Put your BookShop.jar in JBOSS_HOME/server/.../deploy to deploy it into EJB container.
You can annoate your ejb bean using @RemoteBinding:
for example:
@Stateless
@RemoteBinding(jndiBinding = "/BookShop/OperazioniUtenti/remote")
public class TesBean implements ....
and after that you may use lookup inside your sevlet:
initialContext.lookup("/BookShop/OperazioniUtenti/remote");
-
6. Re: lookup a session bean inside a servlet
bertoz84 May 3, 2011 9:49 AM (in response to andi-g)I would prefer to have my .jar inside my web project ( as I do now ) and in the end create an .ear that I will deploy to the server ( my .jar alone is useless without the web interface ). Moreover this is a university project and I need to deliver a totally builded and working .ear.
As i do now I can find every bean and entity in my servlet during code writing; at run time is the same, the only thing is the lookup.
I don't get why everything is seen properly but the lookup returns a null pointer.
I think I'm missing something about the jndi: I got a similar problem while writing my BookShop and I solved it adding the jndi.properties file.
Regard your code:
@Stateless
@RemoteBinding(jndiBinding = "/BookShop/OperazioniUtenti/remote")
public class TesBean implements
I don't get where I have to insert the @RemoteBinding annotation: inside my servlet? or in my session beans ( OperazioniUtenti, etc... ) inside my ejb project BookShop?
I wonder if my BookShop.jar NEEDS to be deployed inside the server to let the lookup work inside my servlet...
-
7. Re: lookup a session bean inside a servlet
wdfink May 3, 2011 12:39 PM (in response to bertoz84)@Stateless and @RemoteBinding are annotations of the stateless session bean.
You should add
@RemoteBinding(jndiBinding = "/BookShop/OperazioniUtenti/remote")
to your bean implementation class.
-
8. lookup a session bean inside a servlet
andi-g May 3, 2011 12:55 PM (in response to bertoz84)Is OperazioniUtenti an EJB component ?
If so, then u have to deploy it as EJB project.
As fare as I know, when u put it in WEB-INF/lib direcory the EJB Container doesn't deploy it.
Could you show me the source code of OperazioniUtenti (just declaration and annotations)
@RemoteBinding - define your own JNDI name to component (e.g. EJB, MBean)
-
9. lookup a session bean inside a servlet
bertoz84 May 3, 2011 2:10 PM (in response to andi-g)Here's my BookShop ( EJB project ) structure:
- Book entity bean
- Utenti entity bean ( in english: Users )
- OperazioniUtenti stateless session bean ( in english: UsersOperations )
- OperazioniLibri stateless session bean ( in english: BooksOperations )
- Carrello stateful session bean ( in english: Cart )
- AccessoUtente stateful session bean ( in english: UserAccess )
this is the code:
package bookshop.library.sessionbean;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import bookshop.library.entitybean.*;
@Stateless
public class OperazioniUtenti implements OperazioniUtentiLocal{
@PersistenceContext
EntityManager em;
public static final String RemoteJNDIName = OperazioniUtenti.class.getSimpleName() + "/remote";
public static final String LocalJNDIName = OperazioniUtenti.class.getSimpleName() + "/local";
public OperazioniUtenti() {
}
bla bla bla....
i have no problem in looking up it in my BookShop project ( inside AccessoUtente bean or in a simple testing java client )
Now again my somethingServlet inside my Web project:
package webinterface.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bookshop.library.entitybean.*;
import bookshop.library.sessionbean.*;
public class somethingServlet extends HttpServlet implements Servlet {
static final long serialVersionUID = 1L;
static Context context;
static OperazioniUtenti opUser;
static Utenti user;
public somethingServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try
{
context = new InitialContext();
System.out.println(OperazioniUtentiLocal.class.toString()); // print successfully INFO [STDOUT] interface bookshop.library.sessionbean.OperazioniUtentiLocal
opUser = (OperazioniUtenti) context.lookup("/BookShop/OperazioniUtenti/local");
} catch (NamingException e) {
e.printStackTrace();
}
bla bla bla...
( ....I switched to Local from Remote....).
Lookup returns a null pointer because it can't find BookShop.
I read a lot about this problem but the best answer i found says to put my BookShop.jar inside MYWEBPROJECTPATH/WebContent/WEB_INF/lib and be sure to have it in the java build path of the web project ( that's what i did ). Here the links:
http://devpinoy.org/blogs/lamia/archive/2008/01/03/deploying-your-jar-with-your-war-in-eclipse.aspx
-
10. lookup a session bean inside a servlet
andi-g May 3, 2011 3:12 PM (in response to bertoz84)Here is a part of a text from second link:
"Mechanism 1: The WEB-INF/lib Directory
If you place a library JAR file (for example, struts.jar) into the WEB-INF/lib directory of the WAR file, a web application or web module can use the Struts APIs. bla bla bla ... This mechanism cannot be used by EJB modules."
I'm not an expert, but as far as I know, there are at least two kinds of containers in JBoss, WebContainer and EJBContainer. If you place BookShop.jar in WEB-INF/lib dir, then ejbDeployer doesn't create EJB components and doen't put theirs names in JNDI.
Try to do so, and put BookShop.jar in deploy dir. Of course, if you want to have everything in one archive, the ear will be the best solution.
-
11. lookup a session bean inside a servlet
bertoz84 May 4, 2011 2:37 AM (in response to andi-g)Mechanism 1....if i'm not a dumb my .JAR is used by a web module, not an ejb module.
I thought that deploying my web project together with my .jar could cause the .jar to be deployed too, or at least set the jndi to point to my ejbs inside my .jar. Maybe that's not true.
I'll try your solutions guys...
-
12. Re: lookup a session bean inside a servlet
bertoz84 May 5, 2011 7:22 AM (in response to andi-g)one step ahead!
I created the .ear ( EARShop ) that "uses" both the .jar ( BookShop ) and the .war ( WebShop ).
I removed the .jar from the WEB-INF/lib inside the .war ( because during .ear deploy there was a duplicate name error ); to include my beans in my web project i simply added under its properties, to the JavaBuildPath/Projects, my BookShop project.
now while deploying the .ear i can read this:
12:37:25,110 INFO [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
EARShop/AccessoUtente/local - EJB3.x Default Local Business Interface
EARShop/AccessoUtente/local-bookshop.library.sessionbean.AccessoUtenteLocal - EJB3.x Local Business Interface
12:37:25,113 INFO [org.jboss.ejb3.session.SessionSpecContainer] Starting jboss.j2ee:ear=EARShop.ear,jar=BookShop.jar,name=Carrello,service=EJB3
12:37:25,114 INFO [org.jboss.ejb3.EJBContainer] STARTED EJB: bookshop.library.sessionbean.Carrello ejbName: Carrello
12:37:25,118 INFO [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
EARShop/Carrello/local - EJB3.x Default Local Business Interface
EARShop/Carrello/local-bookshop.library.sessionbean.CarrelloLocal - EJB3.x Local Business Interface
12:37:25,120 INFO [org.jboss.ejb3.session.SessionSpecContainer] Starting jboss.j2ee:ear=EARShop.ear,jar=BookShop.jar,name=OperazioniLibri,service=EJB3
12:37:25,121 INFO [org.jboss.ejb3.EJBContainer] STARTED EJB: bookshop.library.sessionbean.OperazioniLibri ejbName: OperazioniLibri
12:37:25,125 INFO [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
EARShop/OperazioniLibri/local - EJB3.x Default Local Business Interface
EARShop/OperazioniLibri/local-bookshop.library.sessionbean.OperazioniLibriLocal - EJB3.x Local Business Interface
12:37:25,125 WARN [org.jboss.ejb3.TimerServiceContainer] EJBTHREE-2193: using deprecated TimerServiceFactory for restoring timers
12:37:25,127 INFO [org.jboss.ejb3.session.SessionSpecContainer] Starting jboss.j2ee:ear=EARShop.ear,jar=BookShop.jar,name=BookTestBean,service=EJB3
12:37:25,128 INFO [org.jboss.ejb3.EJBContainer] STARTED EJB: bookshop.test.BookTestBean ejbName: BookTestBean
12:37:25,133 INFO [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
EARShop/BookTestBean/remote - EJB3.x Default Remote Business Interface
EARShop/BookTestBean/remote-bookshop.test.BookTestBeanRemote - EJB3.x Remote Business Interface
EARShop/BookTestBean/local - EJB3.x Default Local Business Interface
EARShop/BookTestBean/local-bookshop.test.BookTestBeanLocal - EJB3.x Local Business Interface
12:37:25,134 WARN [org.jboss.ejb3.TimerServiceContainer] EJBTHREE-2193: using deprecated TimerServiceFactory for restoring timers
12:37:25,135 INFO [org.jboss.ejb3.session.SessionSpecContainer] Starting jboss.j2ee:ear=EARShop.ear,jar=BookShop.jar,name=OperazioniUtenti,service=EJB3
12:37:25,136 INFO [org.jboss.ejb3.EJBContainer] STARTED EJB: bookshop.library.sessionbean.OperazioniUtenti ejbName: OperazioniUtenti
12:37:25,138 INFO [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
EARShop/OperazioniUtenti/local - EJB3.x Default Local Business Interface
EARShop/OperazioniUtenti/local-bookshop.library.sessionbean.OperazioniUtentiLocal - EJB3.x Local Business Interface
12:37:25,139 WARN [org.jboss.ejb3.TimerServiceContainer] EJBTHREE-2193: using deprecated TimerServiceFactory for restoring timers
12:37:25,147 INFO [org.jboss.web.tomcat.service.deployers.TomcatDeployment] deploy, ctxPath=/WebShop
12:37:47,745 INFO [STDOUT] interface bookshop.library.sessionbean.OperazioniUtentiLocal
that means i should lookup my ejbs with this name:
EARShop/OperazioniUtenti/local
( EAR_NAME/bean/local, not JAR_NAME/bean/local );
i tried it and i found out that it doesn't work; instead it works if i use:
EARShop/OperazioniUtenti/local-bookshop.library.sessionbean.OperazioniUtentiLocal
Why are there 2 jndi binding, non-default and default, and here i need to use the default while testing my .jar i use the non-default?
Thank you a lot and sorry for bothering you so much!!!
-
13. lookup a session bean inside a servlet
andi-g May 5, 2011 9:42 AM (in response to bertoz84)No problem
Try to use:
@Stateless
@LocalBinding(jndiBinding = "myProject/OperazioniUtenti/local")
public class OperazioniUtentiBean implements OperazioniUtenti
then you'll be able to write in your servlet:
lookup("myProject/OperazioniUtenti/local")
For remote:
@Remote
@Stateless
@RemoteBinding(jndiBinding = "myProject/OperazioniUtenti/remote")
public class OperazioniUtentiBean implements OperazioniUtenti
Remember to use @Remote while using @RemoteBinding.
Good idea (in my opinion) is to create two separated jars:
1. BookShop.jar or BookShopImpl.jar to keep Beans' implementations (EJB)
2. BookShopApi.jar to keep only interfaces
After that you can safely put BookShopApi to WEB-INF/lib dir or include to other client's application.