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

2

4

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

Morehouse answered 23/1, 2011 at 12:59 Comment(0)
F
7

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:

Foust answered 23/1, 2011 at 18:52 Comment(2)
but business logic could be placed in servlet also and for persistent objects java beans may be used, then what is more in the EJBs?.. and I am not using a relational database but NoSQL(Cassandra database) thus JDBC would not be useful in my case, I think I will have to manage my database connections directly through my servlets(?!)Morehouse
Look at my two previous answers. EJB themselves do not replace java beans for persistent objects. They help you in working with the entity manager in JPA. Use them for implementing DAOs or Services that retrieve JPA entities using the entity manager. I haven't used Cassandra, but even then you would still have your business logic in separate classes and not directly in your Servlets. EJBs also feature declarative security, pooling, concurrency control, asynchronous execution and remoting, which might be handy as well when using Cassandra.Foust
O
0

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.

Opportunist answered 23/1, 2011 at 14:28 Comment(4)
Thanks nrobey! However, I have a doubt; In web applications too there is a requirement for database storage, so will EJBs be required for those database backed applications that require great scalabilty and performance or just servlets + JSP can do fine without any concerns? Thanks.Morehouse
EJBs are not REQUIRED for database communication either, in fact not required for much of anything. Database performance and scalability is mostly a function of how you build and query the database using SQL, not on tools. Servlets and JSP can handle this fine, no problem.Opportunist
EJBs and Servlets are not alternatives. Java web development is always based on servlets at the point where the client HTTP requests hit the server. EJBs can be used by servlets to implement things like declarative transactions and security, and clustering. @Marcos: EJBs can make scalability on the level of app server clusters easier than most alternatives. Of course, that won't help much unless the underlying database can take the load as well.Epicene
Thanks for correcting me Michael! I was only giving technologies that are used for "Web-applications" as asked in the original questions. I didn't intend to imply they are "alternatives" to each other. My apologies for being unclear.Opportunist

© 2022 - 2024 — McMap. All rights reserved.