栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

检查servlet和jsp中的会话

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

检查servlet和jsp中的会话

你可以用测试

HttpServletRequest#getSession(booleancreate)
create=false
。如果尚未创建,它将返回null。

HttpSession session = request.getSession(false);if (session == null) {    // Session is not created.} else {    // Session is already created.}

如果您实际上确实想创建会话(如果它不存在),那么只需抓住它并使用

HttpSession#isNew()
以下命令测试新鲜度:

HttpSession session = request.getSession();if (session.isNew()) {    // Session is freshly created during this request.} else {    // Session was already created during a previous request.}

那就是您在Servlet中要做的。在JSP中,您只能在JSTL和EL的帮助下测试新鲜度。您可以抓住会话

PageContext#getSession()
,然后再调用
isNew()
它。

<c:if test="${pageContext.session.new}">    <p>Session is freshly created during this request.</p></c:if>

要么

<p>Session is ${pageContext.session.new ? 'freshly' : 'already'} created.</p>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/412668.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号