为什么 JSP(和 JSTL/EL)采用与 Servlet 或 JSF 不同的方式来执行资源注入?
为了使Servlet、JSP、JSF等能够使用远程EJB,执行资源注入是必不可少的。然而,JSP 采用与其他方法不同的方式将 EJB 注入其中。让我们通过示例来看看它们。
在 JSF 中,可以通过非常简单的方式注入远程 EJB,如以下 ManagedBean 中所示。 EJB 可以是有状态的、无状态的或单例的。
假设会话bean 实现的接口是CustomerBeanRemote,
@ManagedBean
@RequestScoped
public class AddSubscription
{
@EJB
private CustomerBeanRemote obj=null;
//Getters and setters.
}
这里远程EJB 只能在接口声明之前注入一个注释@EJB。由于此处注入了 EJB,因此即使显式指定为 null,接口也能够调用会话 bean 中的方法。 null 这里根本没有任何意义。
Servlet 采用相同的视图,如下所示。
public class Add extends HttpServlet {
{
@EJB
private CustomerBeanRemote obj=null;
//Rest of the elegant Servlet code goes here.
}
然而,JSP(和 JSTL/EL)采取不同的观点,如下所述。
try
{
obj=(CustomerBeanRemote )new
InitialContext().lookup(CustomerBeanRemote.class.getName());
}
catch(NamingException e)
{
out.println(e.getMessage());
}
obj.someEJBMethod();
或者
try
{
obj=(CustomerBeanRemote )new
InitialContext().lookup("Specific JNDI");
}
catch(NamingException e)
{
out.println(e.getMessage());
}
obj.someEJBMethod();
为什么 JSP(或 JSTL/EL)需要 JNDI(Java 命名和目录接口)来执行资源注入,而在 Servlet 和 JSF 中,只需使用一个注释(@EJB)即可简单地执行资源注入?
To enable Servlet, JSP, JSF etc to use a remote EJB, performing resource injection is essential. JSP however takes a different way to inject an EJB into it than that taken by the rest. let's see all of them by example.
In JSF, a remote EJB can be injected in a very simple way as can be seen in the following ManagedBean. An EJB may a stateful, a stateless or a singleton.
Suppose the interface which is implemented by the session bean is namely CustomerBeanRemote
@ManagedBean
@RequestScoped
public class AddSubscription
{
@EJB
private CustomerBeanRemote obj=null;
//Getters and setters.
}
Here the remoter EJB can be injected only with one annotation @EJB just before the interface declaration. Since EJB is injected here, the interface is able to call the methods in the session bean even if it is explicitly assigned null. null here makes no sense at all.
Servlet takes the same view as shown below.
public class Add extends HttpServlet {
{
@EJB
private CustomerBeanRemote obj=null;
//Rest of the elegant Servlet code goes here.
}
JSP (and JSTL/EL) however takes a different view as mentioned below.
try
{
obj=(CustomerBeanRemote )new
InitialContext().lookup(CustomerBeanRemote.class.getName());
}
catch(NamingException e)
{
out.println(e.getMessage());
}
obj.someEJBMethod();
or
try
{
obj=(CustomerBeanRemote )new
InitialContext().lookup("Specific JNDI");
}
catch(NamingException e)
{
out.println(e.getMessage());
}
obj.someEJBMethod();
Why JSP (or JSTL/EL) requires a JNDI (Java Naming and Directory Interface) to perform a resource injection while in Servlet and JSF it can simply be performed with only one annotation which is @EJB?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JSP 是一个 servlet,因此您当然应该能够在 JSP 中使用注释:(
尽管未经测试)。
但即使它不起作用,这也不是问题,因为 JSP 无论如何都不应该包含 Java 代码。 JSP 应该用于从由 Java 编写的控制器(Servlet 或您首选框架的操作)准备的 bean 生成标记。
请参阅如何避免 JSP 文件中出现 Java 代码?
另外,您的代码片段是错误的:尽管在初始化它们时发生了命名异常,但它们使用了 obj 变量。这显然会导致 NullPointerException。
A JSP is a servlet, so you should certainly be able to use the annotation in the JSP:
(untested though).
But even if it doesn't work, that's not a problem because JSPs should not contain Java code anyway. JSPs should be used to generate markup from beans prepared by a controller (Servlet or action of your preferred framework) written in Java.
See How to avoid Java code in JSP files?
Also, your snippets of code are wrong: they use the
obj
variable although a naming exception occurred when initializing them. This will obviously lead to a NullPointerException.