开发者

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

Are EJBS used in database backed w开发者_开发技巧ebsites(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:

  • Database table access via JPA Vs. EJB in a Web-Application
  • EJB 3 or Hibernate 3

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

  • Frameworks for Layering reusable Architectures


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜