第一次登录页面时,若在表单中选择了“记住密码”,则下次登录网站时不用在填写表单,这里用“success.jsp”表示要打开的页面。若本地Cookie文件中已经保存了验证信息,则会显示已登录,否则会显示没登录。
具体如下所示:
<!--[if !supportLists]-->① <!--[endif]-->.填写表单,选择记住密码“一天“,代码文件为:login.jsp
<!--[if !supportLists]-->② <!--[endif]-->.按下提交后,客户锻页面跳到check.jsp,实际上在服务端已经跳到了“success.jsp”页面。
<!--[if !supportLists]-->③ <!--[endif]-->.新打开一个浏览器,然后直接打开“success.jsp”页面,此时提示已登录。说明Cookie起到了自动登录的作用。
若第①步中,在记住密码项中选择“不保存”则登录“success.jsp”页面效果如下:
二. 对应代码
1.login.jsp
- <%@ page contentType="text/html;charset=GBK"%>
- <mce:script language="javaScript"><!--
- function validate(f){
- if(!(/^/w{5,15}$/.test(f.userId.value))){
- alert("用户id必须为5-15位!");
- f.userId.focus();
- return false;
- }
- if(!(/^/w{5,15}$/.test(f.password.value))){
- alert("密码必须为5-15位!");
- f.password.focus();
- return false;
- }
- return true;
- }
-
- <form action="check.jsp" method="post" onSubmit="return validate(this)">
- <table border="0">
- <tr>
- <td>登录程序</td>
- </tr>
- <tr>
- <td>
- 用户id:
- </td>
- <td>
- <input type="text" name="userid">
- </td>
- </tr>
- <tr>
- <td>
- 密 码:
- </td>
- <td>
- <input type="password" name="password">
- </td>
- </tr>
- <tr>
- <td>
- 记住密码?
- </td>
- <td>
- <select name="savetime">
- <option value="0" selected>不保存</option>
- <option value=<%=24*60*60%>>一天</option>
- <option value=<%=24*60*60*7%>>一周</option>
- </select>
- </td>
- </tr>
- <tr>
- <td> <input type="submit" value="确认"></td>
- <td> <input type="reset" value="重置"></td>
- </tr>
- </table>
- </form>
2.check.jsp
- <%@ page import="java.sql.*"%>
- <%!
- public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
- public static final String DBURL = "jdbc:mysql://localhost:3306/jspDemo" ;
- public static final String DBUSER = "root" ;
- public static final String DBPASS = "root" ;
- %>
- <%
- Connection conn = null ;
- PreparedStatement pstmt = null ;
- ResultSet rs = null ;
- boolean flag = false ;
- %>
- <%
- String userid = request.getParameter("userid") ;
- String password = request.getParameter("password") ;
- String savetime=request.getParameter("savetime");
- System.out.println(savetime);
- try{
- Class.forName(DBDRIVER) ;
- conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
- String sql = "SELECT id FROM tuser WHERE id=? AND password=?" ;
- pstmt = conn.prepareStatement(sql) ;
- pstmt.setString(1,userid) ;
- pstmt.setString(2,password) ;
- rs = pstmt.executeQuery() ;
- if(rs.next()){
-
- flag = true ;
- session.setAttribute("userid",userid);
-
- Cookie cid=new Cookie("userid",userid);
- Cookie cpass=new Cookie("password",password);
- cid.setMaxAge(Integer.parseInt(savetime));
- cpass.setMaxAge(Integer.parseInt(savetime));
- response.addCookie(cid);
- response.addCookie(cpass);
- }
- }catch(Exception e){
- }finally{
- try{
- conn.close() ;
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- %>
- <%
- if(flag){
- %>
- <jsp:forward page="success.jsp"/>
- <%
- }else{
- %>
- <jsp:forward page="failure.jsp"/>
- <%
- }
- %>
3.CookieCheck.jsp
- <%@ page contentType="text/html;charset=GBK"%>
- <%@ page import="java.sql.*"%>
- <%!
- public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
- public static final String DBURL = "jdbc:mysql://localhost:3306/jspDemo" ;
- public static final String DBUSER = "root" ;
- public static final String DBPASS = "root" ;
- %>
- <%
- Connection conn = null ;
- PreparedStatement pstmt = null ;
- ResultSet rs = null ;
- boolean flag = false ;
- %>
- <%
- if(session.getAttribute("userid")==null){
- Cookie[] c=request.getCookies();
- String userid=null;
- String password=null;
- if(c!=null){
- for(int i=0;i<c.length;i++){
- if("userid".equals(c[i].getName())){
- userid = c[i].getValue() ;
- }
- if("password".equals(c[i].getName())){
- password = c[i].getValue() ;
- }
- }
- if(userid!=null&&password!=null){
- try{
- Class.forName(DBDRIVER) ;
- conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
- String sql = "SELECT id FROM tuser WHERE id=? AND password=?" ;
- pstmt = conn.prepareStatement(sql) ;
- pstmt.setString(1,userid) ;
- pstmt.setString(2,password) ;
- rs = pstmt.executeQuery() ;
- if(rs.next()){
-
- flag = true ;
- session.setAttribute("userid",userid);
- }
- }catch(Exception e){
- }finally{
- try{
- conn.close() ;
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- }
- }
- %>
4.succss.jsp
- <%@ page contentType="text/html;charset=GBK"%>
- <jsp:include page="CookieCheck.jsp"/>
- <%
- if(session.getAttribute("userid")!=null){
- %>
- <h1>登陆成功,欢迎光临!</h1>
- <%
- }else{
- %>
- <h1> 您还未登录!</h1>
- <%
- }
- %>
5.failue.jsp
- <%@ page contentType="text/html;charset=GBK"%>
- <h1>登陆失败,请重新<a href="login.html" mce_href="login.html">登陆</a></h1>