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

Reply via email to