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...

