Jan,
How long does your thread take to run? Since you are waiting for the thread
to finish anyway in your servlet, I'm not sure it is very necessary to start
a new thread to process the request. It adds a bit of overhead to process
each request since an extra thread is created for every request that gets
processed. If this is really necessary, then you also might look into using
a thread pool. I don't see the need for the new thread. Perhaps you can
describe what you are doing in this thread. As I suggested in my previous
email, you could use a utility class as an interface between your servlet
and your business logic or just handle everything in methods in your
servlet.

One more thing suggestion is to name your parameters with mor descriptive
names. ie. change this
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException{
    process(arg0, arg1);
}

to 

protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException{
    process(request, response);
}

(you could also use req and resp instead of request and response)

Regards,

Richard

-----Original Message-----
From: Jan Zimmek [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, August 19, 2003 1:17 AM
To: Struts Users Mailing List
Subject: Re: [OT] Question about Servlets & Threads


Hi,

I think you're right, but let me ask a last question please ;-)

here are some source snippets of my classes:

MyServlet (which handles all incoming requests like ActionServlet in struts
and populates Request/Response and the action to execute to the new created
WebThread):
-------------------------------------------------
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException{
    process(arg0, arg1);
}

protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException{
    process(arg0, arg1);
}

protected void process(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException{
    WebThread t = new WebThread(request, response, new WebAction());

    t.start();

    while(t.isAlive());

    t.release();
    t = null;
}

You see all processing is of the request will be done in the "run()" method
of the new created WebThread instance.

And this is the point why I am not understand, that I can run into complex
threading issues.

Snippet of a WebAction-Object (this is always thread-safe like a struts
action):
----------------------------------------------------------------------------
---

public void execute(){
    HttpServletResponse response = Globals.response();

    try{
        response.getWriter().write("<html>****</html>");
    }catch(IOException e){
        e.printStackTrace();
    }
}


And this is the Globals-Object source:
--------------------------------------

public class Globals{
    private static WebThread current(){
        return (WebThread)Thread.currentThread();
    }

    public static HttpServletRequest request(){
        return current().getRequest();
    }

    public static HttpServletResponse response(){
        return current().getResponse();
    }
}

The Globals-class simply returns the Request/Response-Object of the current
WebThread (subclass of Thread) via "getter"-methods. I have tested MyServlet
by making multiple concurrent request by an application (running 10 thread
each making 1000 requests in a loop). Each request has been processed
correctly.

Hope this message is not so long that you directly drop, because I am very
occupied with this subject.

Any comments would be very appreciated.

greets
Jan Zimmek


----- Original Message ----- 
From: "Craig R. McClanahan" <[EMAIL PROTECTED]>
To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Tuesday, August 19, 2003 4:38 AM
Subject: Re: [OT] Question about Servlets & Threads


> On Mon, 18 Aug 2003, Jan Zimmek wrote:
>
> > Date: Mon, 18 Aug 2003 23:47:49 +0200
> > From: Jan Zimmek <[EMAIL PROTECTED]>
> > Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]>
> > To: Struts Users Mailing List <[EMAIL PROTECTED]>,
> >      [EMAIL PROTECTED]
> > Subject: Re: [OT] Question about Servlets & Threads
> >
> > Hi,
> >
> > I know, but I want to have a similar behavior like "global 
> > variables" to access Request/Response-Object without to pass them as 
> > parameter to each method I need to access them.
> >
>
> One option to consider is to store the current request and response 
> objects into a static variable of type java.lang.ThreadLocal (perhaps 
> in a Filter, if you're using Servlet 2.3, or a customized 
> RequestProcessor implementation that does this in its process() method 
> before executing the standard processing).
>
> It's fair to warn you, though, that object oriented purists will frown 
> on anything like this, because static variables (similar to globals in 
> PHP) are the root of all sorts of program bugs :-).  Explicitly 
> passing things around creates *much* safer code.
>
> > Does this mailing support attachments ? Else my problem could be a 
> > bit confusing.
> >
> > greets
> > Jan Zimmek
>
> Craig
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to