随笔-51  评论-14  文章-0  trackbacks-0
附加功能:登录成功后保存Cookie一段时间,在这期间无需重新登录
一个基本的登录模块至少分为4个页面:
1.输入用户信息页面(login.jsp);
2.用户合法性验证页面(check.jsp);
3.登录成功欢迎页面(pass.jsp);
4.登录失败提示页面(failure.jsp)。

为了实现保存Cookie功能,还需增加一个页面:
5.检查Cookie页面(index.jsp)

结构图如下:
---------------------------------------------------------------------

                 index.jsp
                       |
                       |判断Cookie中有无用户名、密码
          ----------------------
          |   Y                 N  |
          |                          V
          |                      login.jsp<--------------------
          |                          |输入用户名、密码           |
          |                          V                                  |
          ---------------->check.jsp                           |
                                      |                                  |
                                      |验证用户名、密码          |
                          ---------------------                     |
                          |  Y                N  |                     |
                          V                       V                    |
                      pass.jsp           failure.jsp----------

---------------------------------------------------------------------

index.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<html>
  
<head>
    
<title>index</title>
  
</head>
  
  
<body>
    
<%
        
int i;
        
//初始化,用于保存Cookie中的用户名、密码
        String C_username="";
        String C_password
="";
        
//获取全部Cookie
        Cookie c[]=request.getCookies();
        
for(i=0;i<c.length;i++)
        {
            
//在Cookie中查找用户名、密码,如果找到,则分别将其赋值给用户名、密码变量
            if("username".equals(c[i].getName()))
                C_username
=c[i].getValue();
            
if("password".equals(c[i].getName()))
                C_password
=c[i].getValue();
        }
        
if(!"".equals(C_username) && !"".equals(C_password))
        {
            
//Cookie中有用户名、密码,将用户名、密码提交到验证页面
            response.sendRedirect("check.jsp?username="+C_username+"&password="+C_password);
        }
        
else
        {
        
//Cookie中没有用户名、密码,跳转到登录页面
    %>
        
<jsp:forward page="login.jsp" />
    
<%
        }
    
%>
  
</body>
</html>

login.jsp:

<%@ page contentType="text/html;charset=GB2312" %>
<html>
  
<head>
    
<title>登录</title>
  
</head>
  
  
<body>
      
<center>
      
<h1>登录页面</h1>
      
<hr>
    
<form action="check.jsp" method="post">
        
<table>
            
<tr>
                
<td>用户名:</td>
                
<td><input type="text" name="username" /></td>
            
</tr>
            
<tr>
                
<td>密  码:</td>
                
<td><input type="password" name="password" /></td>
            
</tr>
            
<tr>
            
<td>Cookie选项:</td>
            
<td>
                
<input type="radio" name="cookie" value="nosave" checked>不保存
                
<input type="radio" name="cookie" value="save">保存1分钟
            
</td>
            
</tr>
            
<tr>
                
<td colspan="2" align="center">
                    
<input type="submit" value="登录" /> 
                    
<input type="reset" value="重置" />
                
</td>
            
</tr>
        
</table>
    
</form>
    
</center>
  
</body>
</html>

 

check.jsp:

<%@ page contentType="text/html;charset=GB2312" %>
<html>
  
<head>
    
<title>验证页面</title>
  
</head>
  
  
<body>
    
<%
        String Username
=request.getParameter("username");
        String Password
=request.getParameter("password");
        String IsCookie
=request.getParameter("cookie");
        
//判断用户名、密码的合法性
        if("magci".equals(Username) && "123456".equals(Password))
        
//为了避免空指向异常,比较两个字符串时,如有字符串常量,则使用字符串常量的“equals”方法(即将常量写在前面)。
        {
            
//合法用户
            if("save".equals(IsCookie))
            {
                
//如果选择了保存Cookie选项,则保存Cookie
                Cookie c1=new Cookie("username",Username);
                Cookie c2
=new Cookie("password",Password);
                
//设置Cookie保存时间为1分钟
                c1.setMaxAge(60);
                c2.setMaxAge(
60);
                response.addCookie(c1);
                response.addCookie(c2);
            }
            
//跳转到欢迎页面
            %>
            
<jsp:forward page="pass.jsp"/>
            
<%
        }
        
else
        {
            
//非法用户,跳转到登录失败页面
            %>
            
<jsp:forward page="failure.jsp" />
            
<%
        }
    
%>
  
</body>
</html>


pass.jsp:

<%@page contentType="text/html;charset=GB2312" %>
<center>
    
<h1>登录成功!!</h1>
    
<hr>
    
<h3>欢迎<font size="12" color="red">
    
<%--forward跳转为服务器端跳转,跳转后仍在check.jsp页面,可以继续使用usename参数 --%>
    
<%=request.getParameter("username"%>
    
</font>光临!</h3>
</center>


failure.jsp:

<%@ page contentType="text/html;charset=GB2312" %>
<div align="center">
<h1>登录失败!!</h1>
<hr>
<a href="login.jsp">重新登录</a>
</div>

posted on 2008-04-21 20:11 Hank1026 阅读(5190) 评论(3)  编辑  收藏

评论:
# re: 使用COOKIE登录验证(转载) 2012-03-21 09:48 | ghg
gfhgfhgf  回复  更多评论
  
# re: 使用COOKIE登录验证asd(转载) 2014-05-13 19:47 | asdas
asdasd  回复  更多评论
  
# re: 使用COOKIE登录验证(转载) 2016-01-04 15:11 | vfdgv
fsfsdvsdfv  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: