【发布时间】:2011-01-25 13:33:36
【问题描述】:
我编写了一个简单的 servlet,我想对其进行测试,以便在我刷新浏览器时保留旧会话。但是,它并没有在我每次刷新页面时创建一个新会话。难道不应该只在我关闭浏览器时创建一个新会话吗? 我使用的是 ServletRunner 而不是在 Tomcat 上运行它,这会是问题吗?
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();
}
}
【问题讨论】: