Java servlet - 会话清理(HttpServletRequest) [英] Java servlet - Session cleanup (HttpServletRequest)

查看:1038
本文介绍了Java servlet - 会话清理(HttpServletRequest)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

General question about java servlets and the best way to handle requests. If I hit my doGet method from a remote server request:

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?
}

在我结束处理请求并生成输出给请求者之后我想基本上杀死他们的会话。目前,该会话徘徊,从而占用内存。然后一旦达到最大值,所有其他调用都会超时。

After I process the request at my end and generate my output to the requester, I want to basically "kill" their session. Currently, that session lingers and thus eats up memory. Then once the max is reached, all other calls are timed out.

我尝试使用请求对象创建一个HttpSession对象,但结果相同:

I tried creating a HttpSession object using the request object, but got the same results:

HttpSession session = request.getSession();
session.invalidate();


推荐答案

HttpSession session = request.getSession(false);
if (session != null) {
    session.invalidate();
}

是文档建议的正确方法。发送新请求后,将创建一个新会话。

is the proper way to go as suggested by the documentation. A new session will be created once the sends a new request.

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

You mentioned that your sessions still take up memory. Do you have any other references to those objects on the session?

您还可以查看: Servlet会话行为和Session.invalidate

这篇关于Java servlet - 会话清理(HttpServletRequest)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆