为什么没有显示用户名和密码

来源:6-4 案例:Cookie在登录中的应用

weixin_慕仙1176837

2020-10-19 13:56

//这是dologin.jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
<h1>登录成功</h1>
<hr>
<br/>
<br/>
<br/>
<%
    //首先判断用户是否选择了记住登录状态
    String[] isUseCookies=request.getParameterValues("isUseCookie");
    if(isUseCookies!=null&&isUseCookies.length>0){
        //把用户名和密码保存在Cookie对象里面
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        Cookie usernameCookie=new Cookie("username",username);
        Cookie passwordCookie=new Cookie("password",password);
        usernameCookie.setMaxAge(864000);
        passwordCookie.setMaxAge(864000);//设置最大生存期限为10天
        response.addCookie(usernameCookie);
        response.addCookie(passwordCookie);
    }else{
        Cookie[] cookies=request.getCookies();
        if(cookies!=null&&cookies.length>0){
            for(Cookie c:cookies){
                if(c.getName().equals("username")||c.getName().equals("password")){
                    c.setMaxAge(0);//设置Cookies失效
                    response.addCookie(c);//重新保存
                }
            }
        }
    }
%>
<a href="users.jsp" target="_blank">查看用户信息</a>
</body>
</html>
//这是user.jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>用户信息</h1>
<hr>
<%
    String username="";
    String password="";
    Cookie[] cookies=request.getCookies();
    if(cookies!=null&&cookies.length>0){
        for(Cookie c:cookies){
            if(c.getName().equals("username")){
                username=c.getValue();
            }
            if(c.getName().equals("password")){
                password=c.getValue();
            }
        }
    }
%>
<br/>
<br/>
<br/>
用户名:<%=username%><br/>
密码:<%=password%><br/>

</body>
</html>
//这是login.jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>用户信息</h1>
<hr>
<%
    String username="";
    String password="";
    Cookie[] cookies=request.getCookies();
    if(cookies!=null&&cookies.length>0){
        for(Cookie c:cookies){
            if(c.getName().equals("username")){
                username=c.getValue();
            }
            if(c.getName().equals("password")){
                password=c.getValue();
            }
        }
    }
%>
<br/>
<br/>
<br/>
用户名:<%=username%><br/>
密码:<%=password%><br/>

</body>
</html>


写回答 关注

3回答

  • weixin_慕仙1176837
    2020-10-19 14:25:08

    已经解决了,代码问题,不过用户名出现了乱码

  • weixin_慕仙1176837
    2020-10-19 14:04:15

     我修改了代码不过只记住了用户名,      密码没有显示出来

  • weixin_慕仙1176837
    2020-10-19 14:03:11

    第三个错了login.jsp是这个

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>用户登录</title>
    </head>
    <body>
    <h1>用户登录</h1>
    <hr>
    <%
        String username="";
        String password="";
        Cookie[] cookies=request.getCookies();
        if(cookies!=null&&cookies.length>0){
            for(Cookie c:cookies){
                if(c.getName().equals("username")){
                    username=c.getValue();
                }
                if(c.getName().equals("password")){
                    password=c.getValue();
                }
            }
        }
    %>
    <form name="loginForm"action="dologin.jsp"method="post">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text"name="username"value="<%=username%>"/></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password"naem="password"value="<%=password%>"/></td>
            </tr>
            <tr>
                <td colspan="2"><input type="checkbox"name="isUseCookie"checked="checked"/>十天内记住我的登录状态</td>
            </tr>
            <tr>
                <td colsapn="2" align="center"><input type="submit" value="登录"/>&#160;&#160;<input type="reset" value="取消"/></td>
            </tr>
        </table>
    </form>
    </body>
    </html>


JAVA遇见HTML——JSP篇

Java Web入门级教程JSP,带你轻松的学习JSP基础知识

248277 学习 · 3071 问题

查看课程

相似问题