从一个jsp到另一个jsp获取String?

时间:2014-10-11 01:52:10

标签: jsp

我首先创建一个html文件来输入Username& Passwword:

<body>
<center>
    <h1>Login Section</h1>
    <form method="post" action ="getInfo.jsp">
        Username <input type="text" name="UserID"><br>
        Password <input type="password" name="pass"><br>
        <input type="submit" name="submit">
    </form>
</center>
</body>

然后first.jsp文件收到用户名&amp;密码(检查cookie,一切运行正常):

<form method="get" action="showInfo.jsp">
    <%      
        String name = request.getParameter("UserID");
        String pass = request.getParameter("pass");
        Cookie pie = new Cookie(name, "nothing_to_do_here");
        pie.setMaxAge(60 * 60 * 7);
        response.addCookie(pie);

        Cookie[] cookies = request.getCookies();
        boolean isLogin = false;
        if (cookies != null) {
            Cookie cookie;
            for (int i = 0; i < cookies.length; i++) {
                cookie = cookies[i];
                if (name.equals(cookie.getName())) {
                    isLogin = true;
                    break;
                }
            }
        }

        if (isLogin == true) {
            out.print("Welcome back " + name);

        } else {
            out.print("Welcome " + name);

        }

    %>

    <a href="showInfo.jsp">Click here</a>
    </form>

点击&#34;点击此处&#34;按钮,我想显示用户名&amp;密码,但它返回NULL。

<body>
<center>
    <h1>Your ID and Password</h1>
    <%
        String id = (String)session.getAttribute((String)"UserID");
        String pass = (String)session.getAttribute((String)"pass");
        out.print(id);
        out.print(pass);
        %>
</center>
</body>

任何人都可以帮我展示一下。

1 个答案:

答案 0 :(得分:0)

嗯,你不是设置那些会话变量,你只是试图获取它们,你需要像 session.setAttribute("UserID", "someuserid");
此外,您应该使用会话来存储登录信息,而不是cookie。客户可以查看和编辑cookie,使用您当前的代码我所要做的就是设置一个cookie说明 Bob : nothing_to_do_here
并且您的申请将假设我是Bob