Bal*_*usC 13
你可以用测试HttpServletRequest#getSession(boolean create)用create=false.如果尚未创建,它将返回null.
HttpSession session = request.getSession(false);
if (session == null) {
// Session is not created.
} else {
// Session is already created.
}
Run Code Online (Sandbox Code Playgroud)
如果你真的想要创建会话,如果它不存在,那么只需抓住它并使用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.
}
Run Code Online (Sandbox Code Playgroud)
这就是你如何在Servlet中做到这一点.在JSP中,您只能在JSTL和EL的帮助下测试新鲜度.您可以通过获取会话PageContext#getSession(),然后只需调用isNew()它.
<c:if test="${pageContext.session.new}">
<p>Session is freshly created during this request.</p>
</c:if>
Run Code Online (Sandbox Code Playgroud)
要么
<p>Session is ${pageContext.session.new ? 'freshly' : 'already'} created.</p>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20214 次 |
| 最近记录: |