• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

query on session attributes

 
Sharath
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a doubt in HFSJ(page no:198/199). It is written tht session attributes are not thread safe. The explanation given for this was tht a client can open multiple browser window at the same time and request for tht servlet, so multiple threads are created and can access session attributes. hence it is not thread safe. it maintains the same session for requests frm multiple browser windows frm a single client.

But i tried this small code just to check it.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
//import wg4.fwk.mvc.SvAncestor;

public class SampleController extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

HttpSession session = request.getSession();
String ss = request.getHeader("Cookie");
response.setContentType("text/html");
PrintWriter out = response.getWriter();

if(session.isNew())
{
out.println("new session created");

}
else
out.println("welcome back to session and ur session id is "+ ss);
}
}


i deployed this and executed.
i opened the browser and I got an output "new session created".
i opened another browser window and again the same output i.e "new session created".

But it should hav given me the output of second else part in the code rt, when i opened the second browser window ?
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe this is browser dependent, and it also depends on how the window was opened. With IE, if you open the window via cntrl-N, the share the same session, while if you open it up by by clicking on the IE icon it's a different session.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To learn Sessions definitely you should understand how browsers behave. IE behaves differently from Firefox. Take a look at the servlet collections link in my signature. This will definitely help you to understand things better.
 
Sharath
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnx guys, i tried cntrl-N and went through servlet collections(by Vishnu). Now i am getting it correctly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic