如何保持 Servlet 会话? [英] How to keep a Servlet session?
问题描述
我编写了一个简单的 servlet,我想对其进行测试,以便在我刷新浏览器时保留旧会话.但是,它并不是每次刷新页面时都会创建一个新会话.只有当我关闭浏览器时才应该创建一个新会话吗?我正在使用 ServletRunner 而不是在 Tomcat 上运行它,这可能是问题吗?
import java.io.*;导入 javax.servlet.*;导入 javax.servlet.http.*;公共类 SessionPlay 扩展了 HttpServlet{public void doGet (HttpServletRequest req, HttpServletResponse resp)抛出 ServletException、IOException{resp.setContentType("text/html");//获取会话对象HttpSession session = req.getSession(true);String id = session.getId();PrintWriter 出;String title = "会话播放";//然后写入响应的数据out = resp.getWriter();out.println("");out.println(标题);out.println(id);out.println("</title></head><body>");out.println("" + title + "
");如果(会话.isNew()){out.println("<p>欢迎新人</p>");out.println("" + id + "
");}别的{out.println("欢迎回来
");out.println("" + id + "
");}out.println("
会话由 cookie 维护.当在服务器中创建一个新会话时,它会将一个 cookie 添加到客户端应该在同一会话中的所有后续请求中发回的响应中.
但是,您显然是通过按 F5 或浏览器中的刷新按钮来刷新初始请求.初始请求不包含会话 cookie.如果您在 HTML 中添加指向页面本身或同一站点上其他页面的链接并单击它,您将看到会话将保持不变.
解决这个问题的方法之一是创建一个过滤器,在创建会话时进行重定向.
<小时>更新:cookies 是可禁用的,并且取决于主机和上下文.您需要确保在网络服务器(例如 Tomcat <Context>
没有 cookies="false"
)和网络浏览器(在设置中的某处)和主机名不是 Intranet 域(某些浏览器不允许在裸主机名上使用 cookie)并且它提交到相同的上下文(webapp 上下文名称).有关如何调试 cookie 的提示,请参阅 这个答案.
与问题无关,在 servlet 中发出 HTML 不是最佳实践.这项工作将由一个 JSP 文件来完成.在完成所有必要的 Java 代码操作后,使用 RequestDispatcher#forward()
将请求转发到 JSP 文件.
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();
}
}
Sessions are maintained by cookies. When a new session is created in server, it will add a cookie to the response which the client is supposed to send back in all subsequent requests in the same session.
However, you're apparently refreshing the initial request by pressing F5 or refresh button in browser. The initial request doesn't contain the session cookie. If you add a link in the HTML which points to the page itself or another page on the same site and click it, you'll see that the session will just be maintained.
One of the ways to go around this is creating a filter which does a redirect when the session is been created.
Update: cookies are disableable and host and context dependent. You need to ensure that cookies are enabled in both webserver (e.g. Tomcat <Context>
doesn't have cookies="false"
) and webbrowser (somewhere in settings) and that the hostname is not an intranet domain (some browsers disallow cookies on naked hostnames) and that it is submitting to the same context (the webapp context name). For hints how to debug cookies, see this answer.
Unrelated to the problem, emitting HTML in a servlet is not the best practice. This job is to be done by a JSP file. Use RequestDispatcher#forward()
to forward the request to a JSP file after doing all the necessary Java code actions.
这篇关于如何保持 Servlet 会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!