You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Clive Welch <mw...@totalise.co.uk> on 2001/10/29 13:40:48 UTC
Servlets, Dates and EJB's
Hi,
I'm making progress in my understanding of all things J2EE with JBoss and
Tomcat.
I'm now hitting a number of design related issues and I'd really appreciate
some pointers either to the solution or mailing lists and/or newsgroups that
cover these issues.
I've got a servlet that handles a form posted to it where some members are
dates. The servlet needs to update a CMP EJB. What's the best approach here?
Do I:
1. Convert the parameter to a date in the servlet and then pass it to the
EJB? This way I can handle the error and return HTML informing the user.
2. Override the set function on the EJB and convert the date in the EJB?
What do I do about errors here? Throw an exception in the set function and
catch it in the servlet?
3. Is direct access to a CMP EJB from the servlet a good idea?
Another servlet provides a list of details - projects in my case. At the
moment there's only a few records but I need to add functionality to provide
pages with say 10 items in each, similar to the results you get from a
search engine.
At the moment I have a stateless session bean that manages a collection of
projects. Implementing the page using this will be relatively
straightforward. But:
4. Is this how its done? Is this the best way? What other approaches are
popular?
Thanks,
Martin
Totalise - the Users ISP
----------------------
To become a member and a shareholder
visit http://www.totalise.net
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Servlets, Dates and EJB's
Posted by Alistair Bayley <al...@abayley.org>.
Clive,
in spite of being relatively new to EJB/Servlets I think I can answer this
with some confidence. You should really buy a copy of "Core J2EE Patterns" by
Alur, Crupi, and Malks (you can get it from amazon). This is an excellent
book and should answer many of your design questions.
You could also check out
http://www.theserverside.com/
for a discussion of J2EE related design issues.
Anyway...
Question 3 is the easiest to answer: no. Do not access entity beans from your
presentation layer. Introduce a layer of stateless session beans (a facade)
which provide the actual functions required (i.e. introduce an abstraction
layer).
Answering 1 and 2:
You should create a Delegate which you create and call from your JSP/Servlet.
This is a plain java class (not an EJB) which will do the JNDI lookup on the
session bean (your facade), do any datatype conversion, call the business
function on the session bean, return any results and/or catch any exceptions.
The session bean could have its own set of exceptions you could be looking
for (these will be business context specific).
The CMP Entity bean should have getters and setters that reflect the actual
datatypes it models (Dates should be Dates, numbers should be numbers, etc).
Any dates, numbers, etc. entered by users will be coming to your JSP/Servlets
as strings. You should convert them to the correct basic datatypes (date,
number, string) in the presentation layer. The session bean facade then
handles conversion of these basic datatypes into business entities.
You already have a session bean that manages a collection of projects. When
this collection is returned to the presentation layer, this will typically be
a collection of a some basic datatype (such as String) or (more likely) a
collection of Value Objects, which are simple java beans - objects with state
(the attributes you want to model) and no behaviour. Value Objects are a
common pattern when passing data structures between the business and
presentation layers (read EJB and JSP/Servlet layers).
Your calling stack might look like this (say for a Create Project use case,
where a form is submitting new project details to a Servlet or JSP):
Browser
|
v
----Presentation Layer----
Servlet/JSP
|
v
CreateProjectDelegate.createProject()
(does JNDI lookup of session EJB, hopefully using a ServiceLocator pattern)
|
v
----Business Abstraction Layer--
CreateProjectSession.createProject()
|
v
ProjectEntity.create()
|
v
----Persistence Layer (Comtainer managed)----
Notes:
1. The CreateProjectSession could raise a CreateProjectException (or
something finer-grained, say ProjectExistsException or
InsufficientPrivilegeException).
2. CreateProjectDelegate could trap exceptions from CreateProjectSession and
convert this to a text message for the Servlet/JSP to display to the user.
The Servlet/JSP only needs to detect when something is wrong, and then it can
interrogate the Delegate for the error details (or it could be passed up to
the Servlet/JSP in another Exception, say CreateProjectDelegateException).
This mechanism can also be used to report errors the Delegate encounters when
performing datatype conversion (from string to date, say).
I realise tis approach seems a bit heavy in terms of volume of code and
classes, but it is the preferred approach. The layers of abstration will make
enhancement and maintenance easier.
Alistair.
---- Original Message: From: Clive Welch Sent: Monday 29 Oct 2001 12:40
Subject: Servlets, Dates and EJB's
Hi,
I'm making progress in my understanding of all things J2EE with JBoss and
Tomcat.
I'm now hitting a number of design related issues and I'd really appreciate
some pointers either to the solution or mailing lists and/or newsgroups that
cover these issues.
I've got a servlet that handles a form posted to it where some members are
dates. The servlet needs to update a CMP EJB. What's the best approach here?
Do I:
1. Convert the parameter to a date in the servlet and then pass it to the
EJB? This way I can handle the error and return HTML informing the user.
2. Override the set function on the EJB and convert the date in the EJB?
What do I do about errors here? Throw an exception in the set function and
catch it in the servlet?
3. Is direct access to a CMP EJB from the servlet a good idea?
Another servlet provides a list of details - projects in my case. At the
moment there's only a few records but I need to add functionality to provide
pages with say 10 items in each, similar to the results you get from a
search engine.
At the moment I have a stateless session bean that manages a collection of
projects. Implementing the page using this will be relatively
straightforward. But:
4. Is this how its done? Is this the best way? What other approaches are
popular?
Thanks,
Martin
Totalise - the Users ISP
----------------------
To become a member and a shareholder
visit http://www.totalise.net
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>