posts - 44,  comments - 48,  trackbacks - 0

          實現 java web 頁面的登錄驗證

          本案例中的程序主要通過 java jdbc-odbc 驅動連接 sql2000 數據庫 , 并依據數據庫中的用戶表信息驗證客戶端登錄請求提交的用戶名和密碼 .

          1.?????? sql2000 數據庫中建立數據庫 test..
          Image000000.jpg

          2.?????? test 數據庫中建表 userid
          ?Image000001.jpg

          3. 在表中增加數據
          ?Image000002.jpg

          3.?????? 建立數據源 test
          ?Image000003.jpg

          ?

          Eclipse 開發(fā)環(huán)境

          4. 新建項目
          ?Image000004.jpg

          4.?????? 新建 WEB 下面的 HTML 頁面 index.html.
          Image000005.jpg

          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 > 系統(tǒng)登錄 </ title >

          </ head >

          < body >

          ??? < center > ???

          ??? < h2 > 系統(tǒng)登錄 </ 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 >

          ?

          界面如右: Image00006.jpg

          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" ); // 驗證通過之后,跳轉到后續(xù)頁面

          ??? 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" > 您可以跳轉到后續(xù)頁面 </ 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 瀏覽器.

          ?

          Image000007.jpg10.?? 測試一下 ^_^

          先輸入用戶名,再輸入密碼,當然只有在 sql2000 中有的用戶才是有較用戶!

          Image000008.jpg
          點擊登陸后跳為下頁:Image000009.jpg

          posted on 2006-08-23 14:57 摩西 閱讀(3890) 評論(3)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網站導航:
           
          主站蜘蛛池模板: 宜宾县| 金湖县| 米脂县| 卓尼县| 洛川县| 绥德县| 文化| 巫山县| 谢通门县| 科尔| 邢台县| 磴口县| 阜康市| 手游| 改则县| 台北市| 镇康县| 思茅市| 东山县| 西丰县| 大埔区| 义马市| 双鸭山市| 白沙| 修武县| 通道| 安阳县| 延川县| 宁海县| 芮城县| 蓬溪县| 木里| 顺昌县| 阳山县| 富源县| 南皮县| 蚌埠市| 秭归县| 云安县| 渑池县| 安乡县|