java HttpServletRequest.getSession(false):什么时候返回null?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10795240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpServletRequest.getSession(false): When does it return null?
提问by AndrewBourgeois
I'd like to know when to expect a "null" when calling HttpServletRequest.getSession(false)
我想知道在调用HttpServletRequest.getSession(false)时什么时候会出现“null”
Also: Are there good tutorials on HttpSession? I'd like to get details like: when to invalidate()? what are the consequences? do I need to check if the returned HttpSession is valid? ... <== Answer that in a comment please.
另外:有关于 HttpSession 的好教程吗?我想获得以下详细信息:何时失效()?后果是什么?我是否需要检查返回的 HttpSession 是否有效?... <== 请在评论中回答。
采纳答案by 0xCAFEBABE
You should practically never expect a null value. As the documentation says, if there is no active session for a request (usually, you would check for that in the filter chain of a request), a session will be created. If you get a null value from this, I'd wager that there is a problem in the underlying OS: Not enough memory to spawn a session would come to mind.
您几乎不应该期望空值。正如文档所说,如果请求没有活动会话(通常,您会在请求的过滤器链中检查它),将创建一个会话。如果你从中得到一个空值,我敢打赌底层操作系统存在问题:没有足够的内存来产生一个会话。