Contact:   Tel: 2726  -  New Media Systems, 1st Floor South, Queens Walk



The simlpest solution for logging into one file by multiple instances of your
servlet might be to write a synchronised method for yuor servlet like this

public synchrronized void log()
{
     // your loggin code
}


For this to work, you also need to define a static FileWriter so that all the
instances of your servlet will share that file writer. This writer should be
only opened once by the first instance of the servlet. Subsequent instance
should check if the file is already open. This can be easily implemented in your
init() method like this

if (theFilerWriter == null)
{
     // create the file writer instance;
}


With regards to the property shared by servlets, if you only want to share the
objects within the instances of one servlet, you might be better off using
synchronised static variables, just like the file writer mentioned above.


Cheers,



Charles








Janet <[EMAIL PROTECTED]> on 09/29/2000 09:00:22 AM

To:        [EMAIL PROTECTED]
cc:         (bcc: Charles Chen/YellowPages)
From:      Janet <[EMAIL PROTECTED]>, 29 September 2000, 9:00 a.m.

How can I share objects between instances of the same Servlet
class?  [Scanned by Yellow Pages PostMaster]


Hello!

Now I'm writing a servlet, say MyServlet, whose instances need to write some
information to a log file of my own. And also they need to access (get and
modify) an object containing information for all of them. So I think I
should do some synchronization operations of this shared object and of
writing to the same log file.

I know the ServletContext class has methods "getAttribute"/"setAttribute"
which allows all the servlets (not just instances of one servlet class) to
share objects. But are these methods synchronized? Or is there any better
way to implement my goal?

And when it comes to writing to the same file, how can I assure there is
just one instance accessing the file at one time?

Any hints and suggestion is appreciated greatly!!!

Best regard:
Janet

___________________________________________________________________________
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

___________________________________________________________________________
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