>>> Leau Stefan Costin <[EMAIL PROTECTED]> 14-May-01 7:38:48 PM >>>
> When can you use a servlet and why and when
> does ejb really helps? Why use ejb and/or servlet?
> And why should anyone make a jsp instead of a
> servlet? - the syntax is much different and uglier and
> they basicly do the same thing.
First: JSP vs Servlets
JSPs are just an easier way to write servlets. They're particularly
usefull if you're not confident with programming languages but you
still want to have some server side functionality.
They're also usefull if you're a java-pro and you just want to do
something quickly.
Servlets should be used whenever you need to do anything complicated.
Complicated things can be done in JSP (there are no limitations) but
the syntax is messy and there aren't many nice IDEs to help you out
(syntax colouring etc...). Also, it's not usual to have the std.
compile-run sequence with JSPs so compile time bugs can be a bit of a
pain with complex code.
EJBs vs Servlets:
This is probably off-topic. But I think it's important to discuss...
I wouldn't dream of voicing these opinions on EJB-interest so here
seems as good a place as any.
This is an opinion which is going to cause some debate... so here
goes. EJBs are similar to JSPs in that they are a qucik and dirty way
of doing something quite complex, in this case, writing business logic
for enterprise apps.
Ideally, the EJB container provides everything the programmer has to
worry about:
- serialization of data
- choice of database
- access to resources
- transactional control
This is tremendously usefull because it means you can just get on
with doing the job at hand and not worry about all the troublesome
(and complex) things that go on in the background.
In comparison a servlet just provides the means to plug some logic
into an HTTP service. Nothing else is provided (well, maybe session
data persistence if the container is so inclined).
But there's nothing that can be done with EJBs that can't be done by
hand inside a servlet. And for some applications hand coding is
better.
Here's an example: I'm working on a web based financial accounts
system (primarily for use at my own company). There's a SQL backend
(of course) which stores all the records of financial transactions.
I've written some objects to represent some of the common
transactions, for example: invoice, credit note, cheque payment,
etc... This sounds like just the sort of thing that I could have
implemented with EJB but I chose not to.
Why? Because EJB doesn't allow me to optimize the database
transactions. An entity bean represents one entity in the database and
has a SQL query that represents that 1:1 binding. But in my accounting
system it is sometimes more efficient to pre-fetch all the entities of
a particular type (eg: all invoices) and to do that using particular
query constraints (eg: all invoices where the supplier is called
"Sun"). EJB cannot represent that kind of relationship (not without
breaking the model anyway).
And the things that EJB provides for me don't actually bring me much.
Because I've thought long and hard about the design I don't have a
problem with transactions (the app is designed to do commits within a
single method). Persistence is not a problem either, I don't need
automatic persistence because it's part of the entire app design.
Access to the database is required to run the app but the driver is
hardcoded because the schema is hardcoded.
In conclusion, EJB is usefull, but IMHO it should be used as a demo
and quick design tool. Use EJB to throw together a working app, once
you've done it you'll understand exactly how the app should work. You
can then go back and implement it putting in lots of extra features
that will improve performance and so on.
Nic Ferrier
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html