你可以用测试
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>


