在什么情况下使用EJB?是否需要在网站/网络应用程序开发? [英] In what situations are EJBs used ? Are they required in websites/ web-application development?

查看:143
本文介绍了在什么情况下使用EJB?是否需要在网站/网络应用程序开发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EJB在数据库支持的网站中使用(所有人都可以访问)?

解决方案

不需要任何东西当然。如果您希望您可以在 CGI 之后的单个大型C函数中构建一个Web应用程序。



EJB确实使Web应用程序开发更容易。 Java EE 6的超轻量级 Web Profile 不包含任何内容。



EJB不包含任何数据库API本身,但它与JPA非常好地集成。您可以在其中注入 EntityManager ,并且自己启动和提交/回滚事务的要求将自动消失。这很大程度上简化了你的代码。



尽管可以将DB相关的代码(JPA或JDBC)直接放入到Servlet甚至JSP页面中,但这是一般的皱眉。 Servlet和JSP用于显示,任何业务或持久性逻辑都不属于该类。一个非常实际的原因是您不能在JSP页面的中间调用重新使用某些业务逻辑。



将业务逻辑分开因此是良好的Web应用程序的主要优点,EJB是Java EE中指定的bean。



有关其他信息,请参阅以下两个类似问题的答案: p>



对于EJB在您的Web应用程序架构的更大图片中的作用:




Are EJBS used in database backed websites(that are accessible to all)?

解决方案

Nothing is ever required of course. If you wanted you could build a web-application as a single large C function behind CGI.

That said, EJBs do make web application development a lot easier. It's not for nothing that they are included in the ultra-lightweight Web Profile of Java EE 6.

EJB does not contain any Database APIs of itself, but it integrates extremely well with JPA. You can inject the EntityManager in it, and the requirement of having to start and commit/rollback transactions yourself disappears. This greatly simplifies your code.

Although you could put DB related code (JPA or JDBC) directly into your Servlets or even JSP pages, this is a practice generally frowned upon. Servlets and JSPs are for display and any business or persistence logic just doesn't belong there. A very practical reason for that is that you can't call into the middle of a JSP page to re-use some piece of business logic.

Keeping your business logic separate is thus a prime virtue of good web applications and EJBs are the designated beans for that in Java EE.

For additional information, see these two answers to similar questions:

For the role of EJB in the bigger picture of your web application architecture:

这篇关于在什么情况下使用EJB?是否需要在网站/网络应用程序开发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆