2010-06-04 29 views
11

Web uygulamamda oturum oturumu zaten var olup olmadığını kontrol etmek istiyorum.sunucuda oturum açma ve jsp

Bunu, sunucu uygulamasında ve jsp'de de kontrol etmek istiyorum. Bunu kontrol etmek için herhangi bir yol var mı?

Teşekkür

cevap

13

Sen create=false ile HttpServletRequest#getSession(boolean create) ile test edebilirsiniz. Henüz oluşturulmamışsa null döndürür. aslında zaten oturumu oluşturmak istiyorsanız o yoksa

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

, o zaman sadece yakala ve HttpSession#isNew() kullanarak tazeliğini test etmek: Bir de bunu yapacağını nasıl oldu

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

Servlet. Bir JSP'de tazeliği sadece JSTL ve EL yardımıyla test edebilirsiniz. Seansı PageContext#getSession()'dan alabilir ve üzerinde sadece isNew()'u arayabilirsiniz.

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

veya

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

bir yolu jsp bir oturum kimliğini ve o zaman hayatta olup olmadığını kontrol etmek için diğer jsp veya servlet'dir aynı oturum kimliğini kontrol etmektir.

HttpSession session = req.getSession(); 
     session.getId();