[英]not able to retrieve session attributes when i navigate through JSP pages
i am working on eclipse juno and apache server. 我正在Eclipse Juno和Apache服务器上工作。
I have 3 pages: 我有3页:
String s_user=request.getParameter("n_user");
HttpSession session=request.getSession(true);
session.setAttribute("ses_user",s_user);
String s =(String)session.getAttribute("ses_user"); System.out.println(s);
i have checked it through 我已经检查了
System.out.println("session ID"+request.getSession(false));
why i am not getting same session ID nor getting the reason for same? 为什么我没有相同的会话ID或相同的原因?
login.jsp: login.jsp:
<form name="loginform" method="post" action="Validation" >
<br><br>
<table width="300px" align="center" style="background-color:#C1CDCD;">
<tr><td colspan=2></td></tr>
<tr><td colspan=2> </td></tr>
<tr>
<td><b><font color="#00008B">Login Name</font></b></td>
<td><input type="text" name="n_user" value=""></td>
</tr>
<tr>
<td><b><font color="#00008B">Password</font></b></td>
<td><input type="password" name="n_pass" value=""></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Submit" onclick="return validate()"></td>
</tr>
<tr> <td colspan=2> </td></tr>
</table>
</form>
System.out.println("session ID" + (String)(request.getSession().getAttribute("ses_user")));
You will get you attribute value set in session. 您将获得在会话中设置的属性值。 SessionId
gets changed because after validation you create new session. SessionId
之所以更改,是因为在验证后您创建了新会话。
//set session
HttpSession session=request.getSession(true);
session.setAttribute("ses_user",s_user);
// get session
String userid = (String)session.getAttribute("ses_user");
if(userid != null){
// do something
}
you should just use HttpSession session=request.getSession()
without boolean parameter. 您应该只使用不带布尔参数的HttpSession session=request.getSession()
。
session=request.getSession()
is same as session=request.getSession(true)
session=request.getSession()
与session=request.getSession(true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.