I have written a simple servlet which I want to test so the old session is kept when I refresh the browser. However it doesn't instead it is creating a new session every time I refresh the page. Isn't it supposed to create a new session only when I close the browser? I'm using ServletRunner instead of running it on Tomcat, can that be the problem?
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SessionPlay extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); //Get session object HttpSession session = req.getSession(true); String id = session.getId(); PrintWriter out; String title = "Session play"; // then write the data of the response out = resp.getWriter(); out.println("<html><head><title>"); out.println(title); out.println(id); out.println("</title></head><body>"); out.println("<h1>" + title + "</h1>"); if(session.isNew()) { out.println("<p>Welcome new comer</p>"); out.println("<p>" + id + "</p>"); } else { out.println("<p>Welcome back</p>"); out.println("<p>" + id + "</p>"); } out.println("<form action=SessionPlay method=get>"); out.println("<input type=input name=param1>"); out.println("<input type=submit>"); out.println("</form>"); out.println("</body></html>"); out.close(); } }