我想实现的功能是:第一次在login.jsp页面输入任意用户名和密码登录后,自动跳转到home.jsp。
第二次打开home.jsp,能够自动登录。
因为我也是参考别人的做的,home.jsp的程序有错误,不知道怎么改。
<%@ page contentType="text/html;charset=GB2312" language="java" import="java.sql.*"
errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html;
charset=GB2312"/>
<title>登录界面</title>
</head><body>
<form id="form1" name="form1" method="post" action="login.jsp">
<p>用户名:<input name="userName" type="text" id="userName"/><p>口令:<input name="pw" type="password" id="pw"/><p> <input type="submit" name="submit" value="登录"/></form>
<%String UserName = request.getParameter("userName");
String UserCode = request.getParameter("pw");
Cookie a=new Cookie("Name1",UserName);
Cookie b=new Cookie("Name2",UserCode);
response.addCookie(a);
response.addCookie(b);
a.setMaxAge(1000*24*60*60);
b.setMaxAge(1000*24*60*60);
<java:forward page="home.jsp"/>
%>
</body>
</html>
<%@ page contentType="text/html;charset=GB2312" language="java" import="java.sql.*"
errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html;
charset=GB2312"/>
<title>主页</title>
</head>
<body>
<%String usernameCookie = null;   
String passwordCookie = null;   
Cookie[] cookies = request.getCookies();   
if (cookies != null) {   
  for (Cookie cookie : cookies) {   
    if ("Name1".equals(cookie.getName())) {   
      usernameCookie = cookie.getValue(); // 得到cookie的用户名   
    }   
    if ("Name2".equals(cookie.getName())) {   
      passwordCookie = cookie.getValue(); // 得到cookie的密码   
    }   
  }   
  if (usernameCookie != null && passwordCookie != null) { // 如果存在   
    if(login.checkLogin(usernameCookie ,passwordCookie)){   
     response.sendRedirect("home.jsp"); // 登陆成功的处理   
    }else{   
       <jsp:forward page="login.jsp"/>  
    }   
  }   
}  
%>
  </body>
  </html>

解决方案 »

  1.   

    1.登录页面中判断Cookie中是否记录了用户名密码<%
    String userName = "";
    String password = ""; Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
           Cookie c = cookies[i];     
           if(c.getName().equalsIgnoreCase("UserName")) {
            userName = c.getValue();
           }
           if(c.getName().equalsIgnoreCase("Password")) {
            password = c.getValue();
           }
        }
    }
    if (userName != null && !userName.trim().equals("") && password != null && !password.trim().equals("")) {
        <java:forward page="home.jsp"/>
    }
    %>
    2.home页面中判断Cookie中记录的用户名密码是否可以登录。可以登录转到home页面;不可以登录清除Cookie,转到登录页面。
    <%
    String usernameCookie = null;  
    String passwordCookie = null;  
    Cookie[] cookies = request.getCookies();  
    if (cookies != null) {  
      for (Cookie cookie : cookies) {  
        if ("Name1".equals(cookie.getName())) {  
          usernameCookie = cookie.getValue(); // 得到cookie的用户名  
        }  
        if ("Name2".equals(cookie.getName())) {  
          passwordCookie = cookie.getValue(); // 得到cookie的密码  
        }  
      }  
      if (usernameCookie != null && passwordCookie != null) { // 如果存在  
        if(login.checkLogin(usernameCookie ,passwordCookie)){  
        response.sendRedirect("home.jsp"); // 登陆成功的处理  
        }else{ 
           Cookie cookie = new Cookie("UserName", null);
    cookie.setMaxAge(0);
    response.addCookie(cookie);
    cookie = new Cookie("Password", null);
    cookie.setMaxAge(0);
    response.addCookie(cookie);
    cookie = new Cookie("Remember", null);
    cookie.setMaxAge(0);
    response.addCookie(cookie);
          <jsp:forward page="login.jsp"/>  
        }  
      }  
    }  
    %>