实现 java web 页面的登录验证
本案例中的程序主要通过 java jdbc-odbc 驱动连接 sql2000 数据库 , 并依据数据库中的用户表信息验证客户端登录请求提交的用户名和密码 .
1. sql2000 数据库中建立数据库 test..
2. 在 test 数据库中建表 userid
3. 在表中增加数据
3. 建立数据源 test
Eclipse 开发环境
4. 新建项目
4. 新建 WEB 下面的 HTML 页面 index.html.
5. 写入代码 :
<! DOCTYPE HTML PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
< title > 系统登录 </ title >
</ head >
< body >
< center >
< h2 > 系统登录 </ h2 >
< form action = "login.jsp" method = "post" >
< Input type = "text" name = "uid" maxlength = 8 style = "width:150" >< br >
< Input type = "password" name = "upwd" maxlength = 8 style = "width:150" >< br >
< Input type = "submit" value = " 登陆" >
< Input type = "reset" value = " 取消" >
</ form >
</ center >
</ body >
</ html >
界面如右:
6. 新建 jsp 文件 login.jsp.
<%@ page language = "java" contentType = "text/html; charset=UTF-8" pageEncoding = "UTF-8" %>
<%@ page import = "java.sql.*" %>
<! DOCTYPE HTML PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
< title > 验证页面 </ title >
</ head >
< body >
<%
String username = request.getParameter( "uid" );
String password = request.getParameter( "upwd" );
if (username != null && !username.equals( "" )){
try {
/*
* 连接数据库
*/
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
Connection con=DriverManager.getConnection( "jdbc:odbc:test" , "" , "" );
Statement stmt =con.createStatement();
String sql = "select * from userid where name='" + username + "'" ;
sql += " and psw='" + password + "'" ; // 准备查询语句
ResultSet rs=stmt.executeQuery( sql );
if ( rs.next() ){
session.setAttribute( "login" , "ok" ); // 验证通过之后,跳转到后续页面
session.setAttribute( "uname" ,username);
%>
< jsp:forward page = "main.jsp" />
<%
} else
out.println( " 错误的用户名和密码" ); // 验证未通过,显示错误信息
out.println( "<a href=index.html> 返回</a>" );
} catch (Exception ee){
ee.printStackTrace();
}
} else {
out.println( " 请先登录!" ); // 验证未通过,显示错误信息
out.println( "<a href=index.html> 返回</a>" );
}
%>
</ body >
</ html >
7. 新建 checkvalid.jsp
<%@ page language = "java" contentType = "text/html; charset=UTF-8" pageEncoding = "UTF-8" %>
<! DOCTYPE HTML PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< title > 验证页面 </ title >
</ head >
< body >
<%
if (session.getAttribute( "login" )== null || !session.getAttribute( "login" ).equals( "ok" )) {
response.sendRedirect( "index.html" ); // 验证没有通过
}
%>
</ body >
</ html >
8. 新建main.jsp
<%@ page language = "java" contentType = "text/html; charset=UTF-8" pageEncoding = "UTF-8" %>
<! DOCTYPE HTML PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
< title > 主页面 </ title >
</ head >
< body >
<%@ include file = "checkvalid.jsp" %>
欢迎进入本页面,您已经通过验证,你的用户名是 <%= session.getAttribute( "uname" ) %> < p >
< A HREF = "continue.jsp" > 您可以跳转到后续页面 </ A >
</ body >
</ html >
9. 新建continue.jsp
<%@ page language = "java" contentType = "text/html; charset=UTF-8" pageEncoding = "UTF-8" %>
<! DOCTYPE HTML PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charsetUTF-8" >
< title > Insert title here </ title >
</ head >
< body >
<%@ include file = "checkvalid.jsp" %>
<%= session.getAttribute( "uname" ) %> , 欢迎您进入第二个页面!
</ body >
</ html >
10. 首先在 Eclipse 中启动 Tomcat 应用服务器 , 然后启动 IE 浏览器.
10. 测试一下 ^_^
先输入用户名,再输入密码,当然只有在 sql2000 中有的用户才是有较用户!
点击登陆后跳为下页: