Sorry, but I think your solution is not completely thread safe, because
you do not declare doGet synchronized. If you did that, it would be
thread safe. But what is the point then? Declare the servlet as
"implements SingleThreadModel", and you are done!

Obviously, you will not avoid other global or out of scope variables
concurrency problems with that.

Yours,

Antonio Fiol


Altankov Peter wrote:

>If you go for the SingleThreadModel, try this to workaround your problem:
>
>        public class TestServlet extends HttpServlet implements SingleThreadModel {
>                ...
>        }
>
>However, 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 and moreover its 
>depricated.(http://jakarta.apache.org/tomcat/tomcat-5.0-doc/servletapi/javax/servlet/SingleThreadModel.html)
>
>In general avoid any global variables in the scope of the servlet class definition 
>unless you know what you are doing. Use method variables instead.
>If you decide to go for the real problem solution, either declare the req and res 
>objects in the, lets say,  doGet method and pass them to the forward method, or keep 
>their declaration in the servlet class but mark the forward method as syncronized 
>(causing threads to enter it one by one).
>
>Here are your hnt snipplets:
>
>// the workaround
>public class TestServlet extends HttpServlet {
>        private HttpServletRequest req;
>        private HttpServletResponse res;
>        [...]
>        public void doGet(HttpServletRequest request, HttpServletResponse response)
>                                                throws ServletException, IOException
>        {
>                [.....]
>                forward("whatever");
>        }
>
>        protected synchronized void forward(String s) {
>                [.. Here is where you refer the global req and res objects ..]
>                [.. but its safe, since the method is marked as synchronized ..]
>        }
>}
>
>-- Or --
>
>// some real solution
>public class TestServlet extends HttpServlet {
>        public void doGet(HttpServletRequest request, HttpServletResponse response)
>                                                throws ServletException, IOException
>        {
>                HttpServletRequest req;
>                HttpServletResponse res;
>                [....]
>                forward("whatever", req, res);
>
>        }
>        protected void forward(String s, HttpServletRequest req, HttpServletResponse 
> res) {
>                [...]
>        }
>}
>
>
>I hope this helps.
>
>
>-----Original Message-----
>From: Philipp Taprogge [mailto:[EMAIL PROTECTED]
>Sent: 11 Декември 2003 г. 16:28
>To: Tomcat Users List
>Subject: Re: response.reset() and forward() ... problematic? DBCP related?
>
>
>Hi!
>
>Antonio Fiol Bonnín wrote:
>
>  
>
>>My guess:
>>
>>req and res are attributes of the Servlet, like in:
>>public class TestServlet extends HttpServlet {
>>private HttpServletRequest req;
>>private HttpServletResponse res;
>>[...]
>>}
>>
>>So you are calling "forward(s)" for a request once req and res have
>>been
>>overwritten by another request.
>>    
>>
>
>Hmm... Im a bit lost here... could anyone perhaps be so kind and post
>a code snipplet of how a thread safe use of a Servlet's request and
>response attribute could look like? I have not been working with
>Servlets for too long and I worried I might run into the same problem.
>  
>

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to