On hitting submit, it takes me to SelectServlet.do where I have the following code in my doGet method...
response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("Testing sessions for " + request.getParameter("Username")); HttpSession session = request.getSession(); if(session.isNew()){ out.println("This is a new session"); }else{ out.println("Welcome back"); }
When I ran it, it always gives me Welcome back..??? Even for the first request, it gives me Welcome back...How is this possible?? I'm confused! Please anyone help me on this.
You are using HttpSession session = request.getSession(); which always returns an already existing session or creates a new session and gives you.Hence session.isNew() is always false.
if you use like this HttpSession session = request.getSession(false);
Roger Stratus
Greenhorn
Joined: Jan 07, 2007
Posts: 3
posted
0
Hi Please read the following Servlet Documentation
isNew public boolean isNew()Returns true if the Web server has created a session but the client has not yet joined. For example, if the server used only cookie-based sessions, and the client had disabled the use of cookies, then a session would be new.
Returns: true if the server has created a session, but the client has not yet joined