posts - 44,  comments - 48,  trackbacks - 0

          實(shí)現(xiàn) java web 頁(yè)面的登錄驗(yàn)證

          本案例中的程序主要通過(guò) java jdbc-odbc 驅(qū)動(dòng)連接 sql2000 數(shù)據(jù)庫(kù) , 并依據(jù)數(shù)據(jù)庫(kù)中的用戶(hù)表信息驗(yàn)證客戶(hù)端登錄請(qǐng)求提交的用戶(hù)名和密碼 .

          1.?????? sql2000 數(shù)據(jù)庫(kù)中建立數(shù)據(jù)庫(kù) test..
          Image000000.jpg

          2.?????? test 數(shù)據(jù)庫(kù)中建表 userid
          ?Image000001.jpg

          3. 在表中增加數(shù)據(jù)
          ?Image000002.jpg

          3.?????? 建立數(shù)據(jù)源 test
          ?Image000003.jpg

          ?

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

          4. 新建項(xiàng)目
          ?Image000004.jpg

          4.?????? 新建 WEB 下面的 HTML 頁(yè)面 index.html.
          Image000005.jpg

          5.?????? 寫(xiě)入代碼 :

          <! 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 > 驗(yàn)證頁(yè)面 </ title >

          </ head >

          < body >

          <%

          String username = request.getParameter( "uid" );

          String password = request.getParameter( "upwd" );

          if (username != null && !username.equals( "" )){

          try {

          ??? /*

          ??? ?* 連接數(shù)據(jù)庫(kù)

          ??? ?*/

          ??????? 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 + "'" ;? // 準(zhǔn)備查詢(xún)語(yǔ)句

          ??? ResultSet rs=stmt.executeQuery( sql );

          ??? if ( rs.next() ){

          ??? session.setAttribute( "login" , "ok" ); // 驗(yàn)證通過(guò)之后,跳轉(zhuǎn)到后續(xù)頁(yè)面

          ??? session.setAttribute( "uname" ,username);

          %>

          ??????? < jsp:forward page = "main.jsp" />

          <%

          ??? } else

          ? ?????? out.println( " 錯(cuò)誤的用戶(hù)名和密碼" );? // 驗(yàn)證未通過(guò),顯示錯(cuò)誤信息

          ??? out.println( "<a href=index.html> 返回</a>" );

          ??? } catch (Exception ee){

          ??? ??? ee.printStackTrace();

          ??? }

          } else {

          ??? out.println( " 請(qǐng)先登錄!" );? // 驗(yàn)證未通過(guò),顯示錯(cuò)誤信息

          ??? 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 > 驗(yàn)證頁(yè)面 </ title >

          </ head >

          < body >

          <%

          ??? if (session.getAttribute( "login" )== null || !session.getAttribute( "login" ).equals( "ok" )) {

          ??????? response.sendRedirect( "index.html" );? // 驗(yàn)證沒(méi)有通過(guò)

          ??? }??????

          %>

          </ 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 > 主頁(yè)面 </ title >

          </ head >

          < body >

          <%@ include file = "checkvalid.jsp" %>

          ??????? 歡迎進(jìn)入本頁(yè)面,您已經(jīng)通過(guò)驗(yàn)證,你的用戶(hù)名是 <%= session.getAttribute( "uname" ) %> < p >

          ??????? < A HREF = "continue.jsp" > 您可以跳轉(zhuǎn)到后續(xù)頁(yè)面 </ 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" ) %> , 歡迎您進(jìn)入第二個(gè)頁(yè)面!

          </ body >

          </ html >

          10. 首先在 Eclipse 中啟動(dòng) Tomcat 應(yīng)用服務(wù)器 , 然后啟動(dòng) IE 瀏覽器.

          ?

          Image000007.jpg10.?? 測(cè)試一下 ^_^

          先輸入用戶(hù)名,再輸入密碼,當(dāng)然只有在 sql2000 中有的用戶(hù)才是有較用戶(hù)!

          Image000008.jpg
          點(diǎn)擊登陸后跳為下頁(yè):Image000009.jpg

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

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 闽清县| 乌兰察布市| 梨树县| 应城市| 柘荣县| 根河市| 万宁市| 阿拉善左旗| 平潭县| 丹凤县| 安泽县| 从化市| 大方县| 两当县| 东城区| 丰镇市| 武安市| 青铜峡市| 巴林右旗| 江山市| 牙克石市| 霍林郭勒市| 中超| 都兰县| 甘洛县| 安乡县| 句容市| 昌江| 久治县| 苍梧县| 乐清市| 威信县| 长宁区| 临泽县| 沂水县| 长顺县| 拉萨市| 任丘市| 乌拉特后旗| 仙桃市| 曲水县|