简体   繁体   中英

In what situations are EJBs used ? Are they required in websites/ web-application development?

EJBS是否用于数据库支持的网站(所有人都可以访问)?

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's are framework constructs ( see here for a high level explanation ).

You can use them in lots of situations, to separate concerns like business logic from other concerns like data storage. A high-profile competitor to EJB's (for contrast) is the Spring framework.

In answer to your question, no, they are not REQUIRED for websites and web application development, though they are a way to do it. Other methods of web development in Java are Servlets , JSP .

Servlets are by far the simplest to code. Its feature limited, but for simple web-apps it's plenty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM