猿问

去掉复选框,页面任然记忆账户和密码,后面页面的查看用户信息,也不是空 .怎么回事?求救

login.jsp 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page import="java.net.*" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>???????</title>
</head>
<body>
    <%   request.setCharacterEncoding("utf-8");
        String username="";
        String password="";
        Cookie[] cookie=request.getCookies();
        if(cookie!=null&&cookie.length>0){
            for(Cookie c:cookie){
                if(c.getName().equals("username")){
                    username=URLDecoder.decode(c.getValue(),"utf-8");
                }
                if(c.getName().equals("password")){
                    password=URLDecoder.decode(c.getValue(),"utf-8");
                }
            }
        }
    %>
   <form 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" name="password" value="<%=password %>"/></td>
         </tr>
         <tr>
           <td colspan="2"><input type="checkbox" name="ischeckbox" checked="checked" value="记住密码"/>记住密码</td>
         </tr>
          <tr>
           <td colspan="2" align="center"><input type="submit" name="submit" value="登录"/></td>
         </tr>
      </table>
   </form>
 
<html>
<head>
 
 
 
<!--dologin.jsp--->
 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page import="java.net.*" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>???????</title>
</head>
<body>
     
    <% request.setCharacterEncoding("utf-8");
    //判断复选框是否被选中
    String[] checkbox=request.getParameterValues("ischeckbox");
    if(checkbox!=null&&checkbox.length>0){
        String username=URLEncoder.encode(request.getParameter("username"),"utf-8");
        String password=URLEncoder.encode(request.getParameter("password"),"utf-8");
         
        //获得Cookie对象并将信息存入cookie对象
        Cookie usernameCookie=new Cookie("username",username);
        Cookie passwordCookie=new Cookie("password",password);
        //保存cookie对象
        response.addCookie(usernameCookie);
        response.addCookie(passwordCookie);
        usernameCookie.setMaxAge(864000);//生存期限10天
        passwordCookie.setMaxAge(864000);
         
    }
    else{
        Cookie[] cookie=request.getCookies();//获得之前保存过的Cookie对象
        if(cookie!=null&&cookie.length>0){
            for(Cookie c:cookie){
                if(c.getName().equals("username")||c.getName().equals("password")){
                c.setMaxAge(0);//销毁对象
                response.addCookie(c);
                }  
            }
        }
         
    }
    %>
    <a href="testlogin.jsp" >查看用户信息</a>  
</body>
</html>
 
 
 
testlogin.jsp
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="java.net.URLDecoder"%>
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>???????</title>
</head>
<body>
    <h1>用户信息</h1>
    <hr>
    <%
    request.setCharacterEncoding("utf-8");
    String username="";
    String password="";
    Cookie[] cookie=request.getCookies();//获取Cookie对象
    if(cookie!=null&&cookie.length>0){
        for(Cookie c:cookie){
            if(c.getName().equals("username")){
                username=URLDecoder.decode(c.getValue(),"utf-8");
                 
            }
            if(c.getName().equals("password")){
                password=URLDecoder.decode(c.getValue(),"utf-8");
            }
        }
    }
     
    %>
    用户名:<%=username %><br>
    密码:<%=password %><br>
     
</body>
</html>


qq_程康_0
浏览 1214回答 1
1回答

一毛钱

去掉复选框,页面任然记忆账户和密码这个问题可能是浏览器的记忆功能造成的,你去掉复选框的时候可以清理一下缓存至于后面的查看详情不为空,也可能是缓存造成的,你可以在<a href="testlogin.jsp?t=时间戳" >查看用户信息</a>  
随时随地看视频慕课网APP

相关分类

Java
我要回答