A shared object is a singleton class that is referenced in a Servlet(s).  To
create a shared object you can use somthing like this

public class Template {

     private Template() { }

     private static Template instance = new Template();

     public staticTemplate getInstance() { return instance; }

     public void generateHTML (String body, String title) {

     out.println("<HTML>");
     out.println("<TITLE>" + title + "<TITLE>");
     out.println("<BODY>");
     out.println(body);
     out.println("</BODY>");
     out.println("</HTML>");

}


This Template object holds the HTML template for a page and recieves body and
title as parameters then writes the page.  This is handy for sites that have the
same look and feel throughout their site by keeping the developer from having to
continuosly include HTML in their sevlets.  It also allows the entire sites look
and feel to be changed once and the changes work across the entire site.  You
acces the shared object like this

//Get and Keep a reference to the shared Template instance
Template tplt = Template.getInstance();

tplt.generateHTML("The Body", "The Title");

The first line gets the instance of Template since it is a singleton class there
is only one and then the second line executes the generateHTML method.

Shared objects are very usefull to encapsulate calculations, data manipulation
and other logic that will be needed often.  The other important this is that it
can save information that can then be accessed by another Servlet allowing
Collaboration between Servlets.  There is a very good chapter on this In Jason
Hunters "Java Servlet Programming".

One side note.   It is important to always keep a reference to the shared object
open at all times in your Servlets or else the garabage collector will come and
clean it up.

Luther



Formanek Gary L <[EMAIL PROTECTED]> on 05/14/99 01:30:43 PM

Please respond to "A mailing list for discussion about Sun Microsystem's Java
      Servlet API Technology." <[EMAIL PROTECTED]>

To:   [EMAIL PROTECTED]
cc:    (bcc: Luther Andal/Avnet)

Subject:  Re: Servlet Chaining




When you speak of a "shared object", what do you mean? I've heard of
components, JavaBeans, Enterprise Java Beans, objects, extending classes,
etc., but what do you mean by a shared object? Is this something you create
using the Servlet API?

Thanks,

Gary

> -----Original Message-----
> From: Luther Andal [SMTP:[EMAIL PROTECTED]]
> Sent: Friday, May 14, 1999 11:14 AM
> To:   [EMAIL PROTECTED]
> Subject:      Re: Servlet Chaining
>
> There is also another possiblity.  You can use the class as a shared
> object so
> that it is cached but it is not a servlet.  This keeps you from having the
> overhead of a Servlet but you have the benefits of persistantcy for speed.
> I
> use shared objects often when I want servlets to communicate with each
> other or
> when I have code that I want to use often but is better off in a component
> so
> that I can reuse it and it has no need to inherit the Servlet properties.
> With
> a shared object you get the best of both worlds.  A persistent object that
> can
> be accessed through the getInstance() method and it does not have the
> overhead
> of a Servlet.
>
> Luther

___________________________________________________________________________
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