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]

Reply via email to