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.
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();
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.
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.
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.
发布评论
评论(3)
不,Tomcat 不是 EJB 容器。只有 Glassfish 3、JBoss AS 6 等是。对于 Tomcat,您必须单独安装它。链接的答案只是一个基本的启动示例。即使没有 EJB,您也可以做到同样的效果。您只需要在bean的初始化、构造或构造后期间自己创建它。例如
或
等等。
请注意,这不是现有代码。这只是您必须自己编写的代码。它包含的只是执行所有数据库交互任务的方法。在支持 EJB 的容器中,您可以使用 JPA 来实现此目的。但在像 Tomcat 这样的简单 servlet 容器上,您需要良好的 JDBC 来实现此目的。您可以在此博客中找到基本 JDBC DAO 的大量启动示例< /a>.
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.
or
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.
截至 2011 年 10 月,Apache 确实有一个 Tomcat 的 Java EE 认证版本
该认证版本称为Apache TomEE 是一个普通的 Tomcat zip 文件,添加了正确的 jar,以便它可以通过 Java EE 6 网络配置文件 TCK。
因此,您可以在 Tomcat 中使用 EJB、JSF、JPA 等,就像在 Apache 提供的 Tomcat 版本上使用 GlassFish、JBoss 等一样。
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.
Apache 是一个 servlet 容器,它只是 Java EE 规范的一部分。一旦找到,@EJB 注释将被跳过,并且 Tomcat 不会进行任何注入。如果您希望 servlet 调用 EJB 方法,则需要在 servlet 启动时进行 JNDI 查找,并将远程接口类放入 servlet 的类路径中。
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.