0

我正在研究 eclipse juno 和 apache 服务器。
我有 3 页:

  • login.jsp(然后将此页面重定向到 servlet(Validation.java) 以进行登录验证,如果有效则导航到 search.jsp)
  • 搜索.jsp
  • fill.jsp

    我已经像这样在 Validation.java 中设置了会话变量的值, 并像这样打印了值: 并在每个页面上打印了会话的值,但是每次当我浏览这些页面时,它都会创建新的sessionID
    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 也没有得到相同的原因?


登录.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>
4

3 回答 3

1

System.out.println("session ID" + (String)(request.getSession().getAttribute("ses_user")));

您将在会话中设置属性值。SessionId被更改,因为在验证后您创建了新会话。

于 2013-10-11T13:24:37.827 回答
0
     //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
}
于 2012-12-17T06:57:23.177 回答
0

你应该只使用HttpSession session=request.getSession()没有布尔参数。

session=request.getSession()session=request.getSession(true)

于 2012-12-17T07:00:06.617 回答