Simple Servlet Thread-Safe Question

So I am going through a number of servlets I threw together for demonstration purposes, and am now going through them to make them thread-safe. I havent done any sort of concurrent programming since college, so I have some fundamental questions....

My question in the following code is, am I correct in my synchronized block covering the majority of this code? It seems logical to me, since the HttpSession is being used initially to get a java bean, and then later again to set another java bean. If there is any code in between these two actions which is not synchronized, wouldnt that give another thread the opportunity to change the session for getting the initial java bean, thus when the other thread goes to set the java bean, it would be setting it for the wrong session?

Am I correct with my logic? I am curious because if this is the case, then the vast majority of several of my servlets will have to be synchronized in one, continuous block. Thankfully, most of them have very little code.

protectedvoid processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{

response.setContentType("text/html;charset=UTF-8");

ShipInfoLocal theBean =new ShipInfoBean();

RequestDispatcher dispatcher = request.getRequestDispatcher("shipInfo/manifests/manifestDisplay.jsp");

synchronized(this){

HttpSession session = request.getSession();

TrackDetailBean detailBean = (TrackDetailBean)session.getAttribute("detailBean");

theBean.setLRNO( detailBean.getImoNumb() );

StoredResultSet s = theBean.getManifestData();

session.setAttribute("manifestData", s);

}

dispatcher.forward(request, response);

}

In fact, what I don't get is the HttpServletRequest and HttpServletResponse are also not thread-safe. What's to say that somewhere down the line, you do a HttpServletRequest.getParameter(), but another thread has already entered the servlet with its own HttpServletRequest. So now you will end up getting the parameter from the 2nd thread's HttpServletRequest, which is logically incorrect.

Basically, I don't understand how you can get away with doing much of anything multithreaded in servlets without making a whole **** method synchronized. The only way I can see how is if you arent manipulating HttpServletRequest, HttpServletResponse, or HttpSession. I don't even see a use for servlets in that case...

[2876 byte] By [Smilera] at [2007-11-26 15:31:43]
# 1

You are way off. The request and response don't need to be thread-safe because only one thread should ever be accessing them at the same time. Another thread processing another request will get its own request and response objects.

If you are worried about 2 requests coming in from the same session (e.g. a user has 2 tabs open in the browser and is simultaneously doing things) and the state of the objects in the session being compromised, you would synchronize on the session. Synchronizing on (this), the instance of the servlet, effectively single-threads the processing.

jleecha at 2007-7-8 21:48:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...