[英]not able to retrieve session attributes when i navigate through JSP pages
我正在Eclipse Juno和Apache服务器上工作。
我有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);
我已经检查了
System.out.println("session ID"+request.getSession(false));
为什么我没有相同的会话ID或相同的原因?
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")));
您将获得在会话中设置的属性值。 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
}
您应该只使用不带布尔参数的HttpSession session=request.getSession()
。
session=request.getSession()
与session=request.getSession(true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.