hi Sourabh and thanks a lot for the reply,
it gives me light

does the way you use the class and use the methods
have any effect in term of garbage collection and possible
memory leaks...

one of my concerns is: if i instanciate a class
in doGet, it is loaded in a thread each time the
servlet executes, so it should automatically
unload when the thread terminates, am i right?
but i saw some code like that inside doGet or doPost

if there is an instance of my class
        use it
else
        create it

is it necessary ? the instance of the class
if that class is instanciated inside doGet
should not exist anymore once doGet terminates

i am confused because there are many ways
to do almost the same thing, well better have
too many ways than none...

alain


On 30 Jan, Sourabh Kulkarni wrote:
> hi,
>
> ----- Original Message -----
> From: alain <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Wednesday, January 30, 2002 4:59 AM
> Subject: what is best practice...
>
>
>> hi all...
>> let's say from a servlet i want to call a class and method in that class
>> could i have an advice about what is the best way to do it
>>
>>
>> ***
>> instanciate the class in the servlet init
>>
>> MyClass mc = null;
>>
>> public void init() throws ServletException {
>>        mc = new MyClass();
>> }
>>
>> processRequest(HttpServletRequest request, HttpServletResponse response) {
>>  ...
>>  mc.doThat(whatever);
>>  ...
>> }
>
>     here take care of multi-threaded execution of the method doThat().
>
>>
>> ***
>> load it dynamically each time the servlet is called
>>
>> processRequest(HttpServletRequest request, HttpServletResponse response) {
>>  ...
>>  Class c = Class.forName(MyClassName);
>>  mc = (MyClass)c.newInstance();
>>  mc.doThat(...);
>> }
>> ***
>>
>     why don't you simply use
>
>     MyClass mc = new MyClass();
>     mc.doThat();
>
>> or use a utility Bean, put it in the servlet context
>> and call the func when i need it
>>
>> ***
>
>     again beware of multi-threaded execution.
>
>> does this all make any difference
>> thanks
>>
>> alain
>>
>
>     IMO it will depend on the objects you are creating. If it is created
> after parsing some XML doc, then doing it each time will be slow. In that
> case I would create it in init and make sure that multi-threaded access
> doesn't create any problems. If it is a light object, then i would prefer to
> create it in doGet() only.
>
> -sourabh
>
> ___________________________________________________________________________
> 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