实现 java web 页面的登录验证
本案例中的程序主要通过 java jdbc-odbc 驱动连接 sql2000 数据库 , 并依据数据库中的用户表信息验证客户端登录请求提交的用户名和密码 .
<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /?><shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?><lock aspectratio="t" v:ext="edit"></lock></shapetype>
Eclipse 开发环境
4. 新建 WEB 下面的 HTML 页面 index.html.
5. 写入代码 :
<! DOCTYPE HTML PUBLIC "-//W<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /?><chmetcnv w:st="on" unitname="C" sourcevalue="3" hasspace="False" negative="False" numbertype="1" tcsc="0">3C</chmetcnv>//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<chmetcnv w:st="on" unitname="C" sourcevalue="3" hasspace="False" negative="False" numbertype="1" tcsc="0">3C</chmetcnv>//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<chmetcnv w:st="on" unitname="C" sourcevalue="3" hasspace="False" negative="False" numbertype="1" tcsc="0">3C</chmetcnv>//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<chmetcnv w:st="on" unitname="C" sourcevalue="3" hasspace="False" negative="False" numbertype="1" tcsc="0">3C</chmetcnv>//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<chmetcnv w:st="on" unitname="C" sourcevalue="3" hasspace="False" negative="False" numbertype="1" tcsc="0">3C</chmetcnv>//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 浏览器.
先输入用户名,再输入密码,当然只有在 sql2000 中有的用户才是有较用户!