When to Use Servlet only, EJB only and, both Servlet And EJB?

I have a question, If I want to build a Web application. Sometime i found that there is no need to use EJB as Servlet already provide that functionality.

Can anyone tell me when do I use Pure Servlet? When do I use Pure EJB and, When do I use Both EJB and Servlet?

I am very interesting in it.

Thanks.

Jack

[352 byte] By [ballclock] at [2007-9-26 4:26:50]
# 1

I recommend reading through the J2EE blueprints and design patterns posting at java.sun.com/j2ee/ ...

There's also a good book on J2EE design patterns that currently out right now, I believe it's listed on the J2EE site as well.

The standard J2EE n-tier implementation recommends that you encapsulate the business logic of the application on the EJB tier and I'd have to agree. You can reuse ejbs in other applications and IIOP clients easier than you can servlets. IMHO I feel that appservers offer more support in terms of transactions, security and scalabilty for ejbs than servlets. (Although I'm sure some would disagree) The J2EE architecture of having the servlets as controllers in the MVC works pretty nicely when you need to extend the application and add in new functionality.

However having said that, there's no absolute answer, it really depends on the application requirements and architecture as well as other factors such as time and money to refactor the existing servlets into EJBs.

I hope I was of some help. ;)

-Dat

DatBean at 2007-6-29 17:36:08 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Thank you for your reply.

After i read your message, you have mentioned that EJB is easier to reuse than Servlet. However, i don't know why EJB is easier to reuse. I think Servlet is also can reuse easily.

For example, i have a servlet which is used to save the Saving account and output the result. I can deploy this servlet in another web server easily, and provide the same functionality.

As a summary, Servlet can do the same functionality to EJB? am i right? the only thing we concern is the development time and performance?

I know Servlet is a single instance process, if multiple clients access to the same Servlet, the performance will slow down... especially when calling SQL statement. As all the thread has to synchronize before accepting other request...

am i right or not...? pls comment...

Jack

ballclock at 2007-6-29 17:36:08 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

> I know Servlet is a single instance process, if

> multiple clients access to the same Servlet, the

> performance will slow down... especially when calling

> SQL statement. As all the thread has to synchronize

> before accepting other request...

> am i right or not...? pls comment...

As far as I know it is not true (at least for IPlanet server) - if there will be concurent requests server will execute more than one instances of servlet.

taganay at 2007-6-29 17:36:08 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

depends on the servlet model but the default implementation is a threaded model meaning the servlet container spawns a thread to handle each request of the service method so requests can execute concurrently.

As for the resuability of servlets, you can certainly put them in another webserver and re-use them but what if your client is not a webpage but an java app? Another scenario is what if other processes want to call your servlet to perform some work? Although servlets can be used outside of a web app context, most aren't. With EJBs, you can have different clients, different EJBs and processes re using them. Another thing that I believe really stands out for EJBs, is their support for transactions. You do not have to manage transactions explicitly like you would in a servlet, this is a awesome feature if you have multiple transactions across several resources, like multiple database servers.

-Dat

DatBean at 2007-6-29 17:36:08 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

EJB and servlets are two different things. Trying to compare them doesn't really make sense. A web application is always going to need some way to process Http requests and this in Java is done accomplished through Servlets (or jsp pages which in turn also use servlets).

EJBs are the basis of distributed cross-platform objects. They are handled through name services and stored in databases. An Entity Bean can be "brought back to life" as long as it's info is stored in a database.Regardless of a server being restarted. All this database support behind an Entity Bean (transactions, queries etc.) are very transparent to the programmer. They are provided by the EJB vendor.

The class properties of a Servlet (or class-level member variables) are shared among all requests (running thread instances of the service method). That's why they should be used with a lot of caution. This feature can be very useful but if used improperly can also be very vulnerable to security and synchronization issues.

I'm not an EJB expert but this explanation should give you some idea of what EJBs are all about.

gduque02 at 2007-6-29 17:36:08 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...