這篇文章主要介紹了jsp獲取action傳來的session和session清空以及判斷,需要的朋友可以參考下
jsp獲取action傳來的session值問題有一下幾種方法:
比如我action中有個(gè)session ("sessionid","sessionValue")
一、用struts標(biāo)簽獲?。?lt;s:property value="#session.sessionid"/>
二、<%=request.getSession.getAttribute("sessionid");> session也是內(nèi)置對(duì)象之一,可以直接用session,比request.getSession方便多了
也可以寫成<%=session.getAttribute("sessionid");>
三、el表達(dá)式獲取:${sessionScope.sessionid}
如果賦值的是個(gè)bean,也是一樣的,類似于${sessionScope.bean.beanProperty}
一般session的時(shí)間是30分鐘,如果判斷session是否存在
代碼如下:
<%
String id = "";
String username = "";
User user = (User)session.getValue("docUser");
//判斷user是否為空,如果不為空才操作以下。不然會(huì)報(bào)空指針異常。
if(user != null){
id = user.getId();
username = user.getUsername();
}
%>
也可以清空session
獲取到session然后把他的值設(shè)置為null
代碼如下:
<%
Lduser lduser = (Lduser) ActionContext.getContext().getSession().get("lduser");
if (lduser == null) {
session.putValue("lduser",null);
out.println("session以關(guān)閉");
%>
更多信息請(qǐng)查看IT技術(shù)專欄