實現
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
"-//W3C//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
"-//W3C//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
"-//W3C//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
"-//W3C//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
"-//W3C//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
中有的用戶才是有較用戶!
點擊登陸后跳為下頁:
posted on 2006-08-23 14:57
摩西 閱讀(3890)
評論(3) 編輯 收藏