Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javaservlet-会话清理(HttpServletRequest)_Java_Session_Servlets_Request - Fatal编程技术网

Javaservlet-会话清理(HttpServletRequest)

Javaservlet-会话清理(HttpServletRequest),java,session,servlets,request,Java,Session,Servlets,Request,关于Javaservlet的一般问题以及处理请求的最佳方法。如果我从远程服务器请求中点击doGet方法: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { .... <do work here> .... kill(request); } private void kill(Http

关于Javaservlet的一般问题以及处理请求的最佳方法。如果我从远程服务器请求中点击doGet方法:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
  ....
  <do work here>
  ....
  kill(request);
}

private void kill(HttpServletRequest request) {
//How do I kill the user session here?
}
试一试

session = request.getSession(false); // so if no session is active no session is created
if (session != null)
  session.setMaxInactiveInterval(1); // so it expires immediatly
是文档中建议的正确方法。客户端发送新请求后,将创建一个新会话

您提到您的会话仍然占用内存。会话中是否有其他对这些对象的引用


如果您不想要会话行为,即在多个请求之间具有状态,您可能还需要查看:

。为什么要创建/使用会话。不要创建会话或在会话中存储任何内容


要确保代码未使用会话,请编写一个请求包装器,该包装器将覆盖
getSession()
方法。

您可以使用

session.removeAttribute("attribute name");

在web.xml中设置一个超时期限

我的印象是,您的具体问题需要以不同的方式解决。最终目标是在处理完会话池中的请求后,结束会话池中的会话。我不确定这是在终止请求对象还是在为该会话创建会话对象以删除它。
HttpSession session = request.getSession(false);
if (session != null) {
    session.invalidate();
}
session.removeAttribute("attribute name");