请教servlet中getsession的用法

发布网友

我来回答

3个回答

热心网友

getSession是返回当前用户的会话对象,参数的区别在于

参数为true,则如果“当前用户的会话对象”为空(第一次访问时)则创建一个新的会话对象返回

参数为false,则如果“当前用户的会话对象”为空,则返回null(即不自动创建会话对象)

注意request.getSession() 等同于 request.getSession(true),除非我们确认session一定存在或者sesson不存在时明确有创建session的需要,否则请尽量使用request.getSession(false)。

热心网友

Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session.
If create is false and the request has no valid HttpSession, this method returns null.

To make sure the session is properly maintained, you must call this method before the response is committed. If the container is using cookies to maintain session integrity and is asked to create a new session when the response is committed, an IllegalStateException is thrown.

Parameters:
true - to create a new session for this request if necessary; false to return null if there's no current session
Returns:
the HttpSession associated with this request or null if create is false and the request has no valid session
See Also:
getSession()

参考资料:J2EE-API

热心网友

去看看文档好了!那是官方的。应该是最准确的

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com