Hi Petra,

of course you can. In fact, you can put near to everything
into the user's session, including EJB references if you like
(though I would hardly recommend that); look up the API
docs for the get/setAttribute() methods of either
HTTPSession or ServletContext for further details.
In fact, you can store anything descending from Object
(with the implicit necessity of typecasting upon retrieval,
just like retrieving something from a Vector or a system
property). So much for the sharing. For storing, I would
recommend using a servlet or (better) action class (de-
pends on your web architecture). The only interesting
question is *what* to store, in the end. This depends
heavily on on your architecture, but I would never
have EJBs directly accessible from the web contai-
ner, not even Session EJBs. From an MVC view, EJBs
are the Model and should be kept de-coupled from
both Presentation and Controller tasks. Upon first
sight, I'd rather have some Controller servlet talking
to the model, retrieving data and storing the results
in a Bean (just a normal Java class holding informa-
tion or a Value Object in EJB terms) for further pro-
cessing. No naming service or whatsoever here. From
an EJB view, the web tier is just another means of
getting input or displaying output. To the EJB
layer, it's nothing more than just another (GUI or
whatever) client and should be handled that way.
Or, to make it short, in an EJB application, the
web tier is for input and output, the EJB part is
for handling the business logic, persistence,
transactions and other things. Usually, commu-
nication between the tiers is implemented via
a servlet handling the details and interfacing
with the EJB part. Still, it's well possible to
store even EJB references in session scope,
but as I said, it's hardly recommendable ;)

HTH a bit,

-- Chris (SCPJ2)

NB. Application and Session scope require
additional knowledge about the web tier,
for there may be thread problems arising.
In any case, be at least aware of these.
I already recommended the Bergsten
book on JSP (O'Reilly). This time, I also
recommend both 'Enterprise Java Beans'
by Monson-Haefel (O'Reilly) and the J2EE
blueprints (available from the Sun site).

----- Original Message -----
From: "petra staub" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, July 05, 2002 3:15 PM
Subject: Servlet context, sharing Java objects


> Beginners question:
>
> How can I share Java objects during a session when the class does not
> implement a servlet interface which would allow to get different context
> scopes (e.g. session,application scopes), e.g. business logic
> java beans?
>
> Can this be done using a naming service and if, is this "the right way"? I
> have seen from EJB examples the construction
> "PortableRemoteObject.narrow(ref,ClassName.class)" which seems
> to be the way to do it (allow generic java beans access sources or
objects).
>
> Does something similar exist in a pure Tomcat, say non-EJB, environment?
> Which recommendations, design patterns exist for
> this subject?
>
> Thank you!

___________________________________________________________________________
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

Reply via email to