11
A
回答
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.
}
如果你真的想反正創建會話,如果不存在的話,那麼就抓住它,並使用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>
2
一種方式是設定在JSP中的會話ID,然後檢查相同的會話ID在其他JSP或Servlet來檢查它是否還活着與否。
HttpSession session = req.getSession();
session.getId();
相關問題
- 1. Servlet和JSP中的會話管理
- 2. 如何在JSP EL中檢查會話?
- 3. 從servlet訪問JSP會話
- 4. 值在JSP/Servlet會話是空
- 5. 在jsp和Servlet Filter中獲取相同的會話對象?
- 6. 獲取JSP servlet會話,返回空值
- 7. 在Servlet中創建會話並在JSP頁面中使用?
- 8. 如何在servlet和jsp文件之間共享會話屬性?
- 9. 在Java中,Hibernate會話,JSP/Servlet會話和會話事務管理之間有什麼區別
- 10. 異常在servlet jsp和servlet中的java.lang.NullPointerException
- 11. 在asp.net中檢查會話?
- 12. 結合$會話和$檢查
- 13. 在php和jsp中的會話ID
- 14. JSP轉發和調用servlet(傳遞2個會話變量)
- 15. JSP頁面和Servlet報告不同的會話ID
- 16. 的JSP Servlet - 如何設置和獲取會話變量
- 17. 訪問相同會話bean的Java servlet和JSP
- 18. Servlet會話行爲和Session.invalidate
- 19. Servlet和jsp
- 20. 在jsp/struts中的會話
- 21. 如何在jsp中創建會話對象和會話變量?
- 22. 檢查會話
- 23. JSP中的會話
- 24. JSP會話和Bean混淆
- 25. 從數據庫中檢索字節組在servlet和JSP中
- 26. servlet和jsp在MVC框架
- 27. 角色在JSP和Servlet
- 28. django會話檢查存在
- 29. Jsp和Servlet處理
- 30. HTTP servlet和jsp 404