Hi,

> -----Original Message-----
> From: David Rault [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, January 31, 2002 11:36 AM
> To: Tomcat Users List
> Subject: Re: multiple init() calls
> 
> 
> hi
> 
> i think there may be a misunderstanding (or i misunderstood your 
> message ?!)
> the servlet must create _only_ one instance of each servlet class per web
> application

I believe there is no such MUST in spec.

> then this instance must receive _one_ message init()
> at this point user request may be served
> for each user request, a thread is started and executes the service(...)
> method on the
> _shared_ instance
> 
> the singleThreadModel only tells the servlet engine not to serve several
> request simultaneously
> this is the behaviour required by the J2EE specs
> 
> as for your problem, i'm doing servlet for about 2 years now and 
> i never saw
> that kind of
> behaviour... may be a look at your code would help...
> 

So, lets turn to spec (Servlet 2.3)

<cite>
SRV.14.2.20 SingleThreadModel
public interface SingleThreadModel
Ensures that servlets handle only one request at a time. This interface has no
methods.
If a servlet implements this interface, you are guaranteed that no two threads will
execute concurrently in the servlet?s service method. The servlet container can
make this guarantee by synchronizing access to a single instance of the servlet, or
by maintaining a pool of servlet instances and dispatching each new request to a
free servlet.
This interface does not prevent synchronization problems that result from servlets
accessing shared resources such as static class variables or classes outside the
scope of the servlet.
</cite>

I believe TC (at least 4.0.2b2) chose the second variant. The pool of instances.
So any instance have to be properly initialized.

just an example

package webtests;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class SingleThread extends HttpServlet implements SingleThreadModel {

    private static int cnt = 0;

    private static final String CONTENT_TYPE = "text/html; charset=windows-1251";

    /**Initialize global variables*/
    public void init() throws ServletException {
        System.err.println("init at " + Thread.currentThread().toString());
        System.err.println("Counter: " + (++cnt));
        System.err.println("Instance: " + this.toString());
    }

    /**Process the HTTP Get request*/
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>SingleThread</title></head>");
        out.println("<body>");
        out.println("<p>The servlet has received a GET. This is the reply.</p>");
        out.println("</body></html>");
    }

}

and TC console:

init at Thread[HttpProcessor[8080][0],5,main]
Counter: 1
Instance: webtests.SingleThread@19dc16
init at Thread[HttpProcessor[8080][0],5,main]
Counter: 2
Instance: webtests.SingleThread@405d3b

BTW, I'm not sure but it looks like TC3.2 creates only one instance.
Sorry, cant use attachments due to some "security" restrictions. 
:E

> have fun!
> 
> David
> 

Anton.

--
To unsubscribe:   <mailto:[EMAIL PROTECTED]>
For additional commands: <mailto:[EMAIL PROTECTED]>
Troubles with the list: <mailto:[EMAIL PROTECTED]>

Reply via email to