@EJB注解可以用于远程调用吗?
public class Servlet2Stateless extends HttpServlet {
@EJB private HelloUserLocal helloUser;
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println(newSess.getName());
}
当我将 EJB 和 Servlet 部署在不同的服务器上时,上面的代码行可以工作吗?或者我需要通过传统方式调用它???
public class Servlet2Stateless extends HttpServlet {
@EJB private HelloUserLocal helloUser;
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println(newSess.getName());
}
will above line of code work when I have EJB and Servlet deployed on different servers? or I need to call it through traditional way????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果 EJB 驻留在与客户端 (Servlet) 不同的服务器上,则无法使用 @EJB 注释进行依赖项注入。
我想您需要采用旧的 JNDI 方式。
If the EJB resides on the different server than your client (Servlet) than you cannot use the dependency injection with @EJB annotation.
I guess that you'll need to go with the old JNDI way.
根据 EJB 3.1 规范,您可以在各种客户端中使用 @EJB 注释,包括您的情况的 servlet。
问题是您在不同的主机上运行客户端和服务器。根据您使用的服务器,您也许能够使用 EJB 注释。 这篇文章解释了如何在 Weblogic 中执行此操作。
不用说,无论哪种情况,您都必须将服务器 EJB 定义为 @Remote。
According to EJB 3.1 spec, you can use @EJB annotation in a variety of clients, including servlets which is your case.
The problem is that you are running client and server in different hosts. Depending on the server you are using, you might be able to use the EJB annotation. This post explains how to do it in Weblogic.
Needless to say you have to define the server EJB as @Remote in either case.
如果您的容器也支持 CDI,您可以为执行 JNDI 查找的 bean 编写一个 CDI 生产者方法。那么你至少可以将查找与注射部位分开。
If your container also supports CDI, you could write a CDI producer method for the bean whcih does the JNDI lookup. Then you can at least separate the lookup from the injection site.
在独立客户端中使用依赖注入怎么样?
What about using dependency injection in a standalone client ?