如何检查会话是否存在? [英] How to check if session exists or not?

查看:91
本文介绍了如何检查会话是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

HttpSession session = request.getSession();

在创建会话之前,我想检查它是否存在。我该怎么做?

Before creating session I want to check if it exists or not. How would I do this?

推荐答案

如果你想在创建之前检查这个,那么这样做:

If you want to check this before creating, then do so:

HttpSession session = request.getSession(false);
if (session == null) {
    // Not created yet. Now do so yourself.
    session = request.getSession();
} else {
    // Already created.
}

如果你不关心之后检查 >创建,然后你也可以这样做:

If you don't care about checking this after creating, then you can also do so:

HttpSession session = request.getSession();
if (session.isNew()) {
    // Freshly created.
} else {
    // Already created.
}

保存一行和布尔 request.getSession() request.getSession(true)

That saves a line and a boolean. The request.getSession() does the same as request.getSession(true).

这篇关于如何检查会话是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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