wood burning stoves 2.0*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Trouble in using @EJB in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Trouble in using @EJB in Servlet" Watch "Trouble in using @EJB in Servlet" New topic
Author

Trouble in using @EJB in Servlet

nitin pai
Ranch Hand

Joined: May 30, 2006
Posts: 185
I am using Glassfish v2 for making EJB 3 sample. I made a stateless session HelloWorld Bean and also a Java client for it. All is working fine. Even making a servlet and performing JNDI lookup in the servlet works.

But when I make a servlet and use @EJB to inject the HelloWorld Bean there is no output nor any error from servlet. When I access the server logs it says only one exception - java.lang.NullPointerException

My question is - Is the web client a managed environment?
Can I use @EJB in servlets and if yes how?



Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Hi nitin,

I have had trouble using the @EJB annotation with servlets on JBoss 4.x. It turned to be a problem with that version's implementation: I installed JBoss 5 beta 4 (yes, it's beta) and it worked fine.

Could you post the relevant code (servlet, EJB, web.xml, application.xml)? How are you deploying (EAR, WAR + EJB-JAR)?
[ May 13, 2008: Message edited by: Sergio Tridente ]

SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
bernard savary
Ranch Hand

Joined: Jan 09, 2008
Posts: 91
Hi ,

I have used @EJB in glasfish to call a EJB from a servlet . It works fine. I think you could be missing something with the JNDI names , Please check the server logs for NamingException. Also I would like to add that DI cannot be used in JSP's and helper classes . We have to use a JNDI Lookup . (Refer page 440 in ejb3 in action )
nitin pai
Ranch Hand

Joined: May 30, 2006
Posts: 185
Originally posted by bernard savary:
Hi ,

I have used @EJB in glasfish to call a EJB from a servlet . It works fine. I think you could be missing something with the JNDI names , Please check the server logs for NamingException. Also I would like to add that DI cannot be used in JSP's and helper classes . We have to use a JNDI Lookup . (Refer page 440 in ejb3 in action )


Thanks for pointing out the source. I will check my code to see if any mistake exists since the book says that servlets can access EJB through DI. But do you know the reason why JSP cannot use DI. It in turn is a servlet by nature.
nitin pai
Ranch Hand

Joined: May 30, 2006
Posts: 185
I am still not able to get any output. This is the servlet code:



While my Java client is working fine with the below code:




The method sayHello of the bean just returns the text "Hello Nitin". Please tell me where am I going wrong.
bernard savary
Ranch Hand

Joined: Jan 09, 2008
Posts: 91
Hi try running it without using the mappedName . Dont use mappedName with @Stateless or @Stateful as well
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Can you post your bean class definition? Are you using the @RemoteBinding annotation?

Also. In JBoss you can define the jndi name inside the jboss.xml file. Can you post the contents of the Glassfish' equivalent?
nitin pai
Ranch Hand

Joined: May 30, 2006
Posts: 185
hey guys, I finally managed to solve this problem and guess what, this wasnt a problem of using the @EJB at all. It was a problem of web.xml! Can you imagine.

I had created a web project in eclipse europa which in turn had created web.xml automatically. After having faced this problem I really was splitting my hairs apart when I came across a statement somewhere on the net. It said that only servlets 2.5 have the annotation processing capability.

When I saw the automatically generated web.xml, bingo... i found that it had the attribute version="2.4". I removed the attribute and voila.... it started working!

I had a good laugh when I saw the output. You can never be sure where the problem lies in the world of Java EE.
Paul Michael
Ranch Hand

Joined: Jul 02, 2001
Posts: 697
Originally posted by Sergio Tridente:
Hi nitin,

I have had trouble using the @EJB annotation with servlets on JBoss 4.x. It turned to be a problem with that version's implementation: I installed JBoss 5 beta 4 (yes, it's beta) and it worked fine.


Had the same problem using @EJB annotation with JBoss 4 not long ago.

I'm glad to hear that you were able to make it work with JBoss 5.


SCJP 1.2 (89%), SCWCD 1.3 (94%), IBM 486 (90%), SCJA Beta (96%), SCEA (91% / 77%), SCEA 5 P1 (77%), SCBCD 5 (85%)
Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 402
try "java:/ejb/MappedName/remote"

Regards
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Trouble in using @EJB in Servlet