This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble in using @EJB in Servlet

 
nitin pai
Ranch Hand
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 329
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
bernard savary
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi try running it without using the mappedName . Dont use mappedName with @Stateless or @Stateful as well
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 697
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chaminda Amarasinghe
Ranch Hand
Posts: 404
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try "java:/ejb/MappedName/remote"

Regards
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic