技巧:JSP登录验证功能的实现

2008-02-23 08:13:29来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

静态的登录界面的设计login.htm,代码如下:

<html>
<head>
<title>系统登录</title>
<style type="text/CSS">...
<!--
.style1 {...}{
font-size: 18px;
font-weight: bold;
}
.style2 {...}{font-size: 24px}
.style5 {...}{font-size: 16px}
-->
</style>
</head>
<body bgcolor="papayawhip" width="300" height="300">
<center>
<table border="2" bordercolor="black" bgcolor="lightgreen">
<tbody>
<tr>
<td><div align="center" class="style1 style2">系 统 登 录
</div></td>
</tr>
<form action="login.jsp" method="post">
<tr>
<td height="28"><span class="style5">用户名</span> <input type="text" name="uid" maxlength="20" style="width:150"></td></tr><br>
<tr>
<td><span class="style5">密&nbsp;&nbsp;码</span> <input type="password" name="upwd" maxlength="20" style="width:150"></td></tr><br>
<center>
<tr><td><div align="center">
<input type="submit" value="登录" >&nbsp;&nbsp;
<input type="reset" value="取消">
</div></td></tr>
</center>
</form>

</tbody>
</table>
</center>
</body>
</html>

  将登录用户输入的信息提交到login.jsp页面机型处理,这里为了方便,不执行数据库的访问操作,直接使用sky2098作为登录用户名和密码,但在实际中是要从数据库中读取的,该jsp页面代码实现如下:

<%...@ page contentType="text/html;charset=GB2312"%>
<%...
if(request.getParameter("uid").equals("sky2098")&&request.getParameter("upwd").equals("sky2098")){
session.setAttribute("login","ok");
session.setMaxInactiveInterval(-1);
%>
<jsp:forward page="main.jsp"/>
<%...
}else{
out.println("用户名或密码输入错误!");
}
%>

  如果登录成功,则设定login的值为ok,提交到下一步验证页面,则进入main.jsp页面,否则,如果输入的用户名和密码不合法就打印错误信息,main.jsp页面代码如下:

<%...@ page contentType="text/html;charset=GB2312"%>
<%...@ include file="checkvalid.jsp" %>
<html>
<head>
<title>~WELCOME TO MY HOMEPAGE~</title>
</head>
<body>
<center>
~WELCOME TO MY HOMEPAGE~
</center>
</body>
</html>

  这个页面使用<% @ include file="checkvalid.jsp" %>包含了一个jsp页面checkvalid.jsp为了验证输入信息的合法性:

<%...
if(session.getAttribute("login")==null||!session.getAttribute("login").equals("ok")){
response.sendRedirect("login.htm");
}
%>

  如果输入信息有误,则回到登录页面,重新输入登录信息。

  测试登录功能。

  启动Tomcat服务器,在IE地址栏中键入URL为:

http://localhost:8080/sky2098/login-Advanced/login.htm

关键词:
【推荐给好友】【关闭】
最新五条评论
查看全部评论
评论总数 0 条
您的评论
用户名: 新注册) 密 码: 匿名:
·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:实例代码:JSP高访问量下的计数程序

下一篇:解决tomcat频繁死掉的问题