0

Can we use @EJB annotation in Apache tomcat?

If we can use @EJB annotation in Apache tomcat then please tell me that what kind of attributes and methods must be there in the service class like the EmployeeService class in the answer of question asked on this link.

Community
  • 1
  • 1
Adnan
  • 4,517
  • 15
  • 44
  • 54

3 Answers3

4

No, Tomcat isn't an EJB container. Only Glassfish 3, JBoss AS 6, etc are. For Tomcat you have to install it separately. The linked answer was just a basic kickoff example. You can do it as good without EJB. You only need to create it yourself during bean's initialization, construction or postconstruct. E.g.

private UserService userService = new UserService();

or

private UserService userService = ServiceFactory.getUserService();

etc.

Note that this isn't existing code. It's just code which you have to write yourself. All it contains are just methods which does all the database interaction task. In an EJB capable container you'd use JPA for this. But on a simple servletcontainer like Tomcat you'd need good ol' JDBC for this. You can find extensive kickoff examples of a basic JDBC DAO in this blog.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
2

As of October 2011 Apache does have a Java EE certified version of Tomcat

The certified version is called Apache TomEE and is a plain Tomcat zip file with the right jars added so that it can pass the Java EE 6 Web Profile TCK.

So you can use EJB, JSF, JPA and more in Tomcat just as you can with GlassFish, JBoss, etc. on a version of Tomcat shipped from Apache.

David Blevins
  • 19,178
  • 3
  • 53
  • 68
0

Apache is a servlet container which is only a part of Java EE specification. Once found, @EJB annotations will be skipped and no injections will be made by Tomcat. If you want your servlet to call EJB methods you will need to make JNDI lookups on servlet startup and put remote interface classes in servlet's classpath.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
andbi
  • 4,426
  • 5
  • 45
  • 70