




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、本例需要的軟件和運(yùn)行環(huán)境:1、Windows2000 Server操作系統(tǒng)2、jdk1.43、JCreator2.5(java源碼編輯調(diào)試器,吐血推薦?。?、Macromedia JRun MX5、Macromedia Dreamweaver MX(非必需)6、MySQL數(shù)據(jù)庫(最好安裝MySQL Control Center) 一、數(shù)據(jù)庫設(shè)計(jì)用MySQL Control Center打開MySQL數(shù)據(jù)庫,新建數(shù)據(jù)庫shopping,在其下新建表tbl_user,其中各字段設(shè)置如下:二、編寫連接數(shù)據(jù)庫bean:DBConn.java/DBConn.java/include required c
2、lassesimport java.sql.*;/=/ Define Class DBConn/=public class DBConn public String sql_driver = "org.gjt.mm.mysql.Driver" public String sql_url = "jdbc:mysql:/localhost:3306" public String sql_DBName = "shopping" public String user = "sa"
3、160;public String pwd = "" Connection conn = null; Statement stmt = null; ResultSet rs = null; public boolean setDriver(String drv) this.sql_driver = drv; return true; public String getDriver() return this.sql_driver;
4、public boolean setUrl(String url) this.sql_url = url; return true; public boolean setDBName(String dbname) this.sql_DBName = dbname; return true; public String getDBName() return this.sql_DBName; public boolean setUser(
5、String user) this.user = user; return true; public String getUser() return this.user; public boolean setPwd(String pwd) this.pwd = pwd; return true; public String getPwd() return this.pwd; public
6、DBConn() try Class.forName(sql_driver);/加載數(shù)據(jù)庫驅(qū)動(dòng)程序 this.conn = DriverManager.getConnection(sql_url + "/" + sql_DBName + "?user=" + user + "&password=" + pwd + "&useUnicode=true&characterEncoding=gb2312");
7、60; this.stmt = this.conn.createStatement(); catch(Exception e) System.out.println(e.toString(); /執(zhí)行查詢操作 public ResultSet executeQuery(String strSql) try
8、60; this.rs = stmt.executeQuery(strSql); return this.rs; catch(SQLException e) System.out.println(e.toString(); return null; catch(NullPointerException e) System.out.println(e.toString(); return null;
9、60; /執(zhí)行數(shù)據(jù)的插入、刪除、修改操作 public boolean execute(String strSql) try if(this.stmt.executeUpdate(strSql) = 0) return false; else return true; catch(SQLE
10、xception e) System.out.println(e.toString(); return false; catch(NullPointerException e) System.out.println(e.toString(); return false; /結(jié)果集指針跳
11、轉(zhuǎn)到某一行 public boolean rs_absolute(int row) try this.rs.absolute(row); return true; catch(SQLException e) System.out.println(e.toString(); return false; public void rs_afterLast() try this.
12、rs.afterLast(); catch(SQLException e) System.out.println(e.toString(); public void rs_beforeFirst() try this.rs.beforeFirst(); catch(SQLException e) System.out.print(e.toString(); public void rs_close()
13、160; try this.rs.close(); catch(SQLException e) System.out.print(e.toString(); public void rs_deleteRow() try this.rs.deleteRow(); catch(SQLException e) System.out.print(e.toString(); pu
14、blic boolean rs_first() try this.rs.first(); return true; catch(SQLException e) System.out.print(e.toString(); return false; public String rs_getString(String column) try return this.rs.getStr
15、ing(column); catch(SQLException e) System.out.println(e.toString(); return null; /此方法用于獲取大段文本, &
16、#160; /將其中的回車換行替換為<br> /輸出到html頁面 public String rs_getHtmlString(String column) try String str1 = this.rs.getString(column); String str2 = "rn&quo
17、t; String str3 = "<br>" return this.replaceAll(str1,str2,str3); catch(SQLException e) System.out.println(e.toString(); return null;
18、160; /把str1字符串中的str2字符串替換為str3字符串 private static String replaceAll(String str1,String str2,String str3) StringBuffer strBuf = new StringBuffer(str1); int index=0; while(str1.indexOf(str2,index)!=-1) index=str1.indexOf(str2,index
19、); strBuf.replace(str1.indexOf(str2,index),str1.indexOf(str2,index)+str2.length(),str3); index=index+str3.length(); str1=strBuf.toString(); return strBuf.toString(); public int rs_getInt(String column) try ret
20、urn this.rs.getInt(column); catch(SQLException e) System.out.println(e.toString(); return -1; public int rs_getInt(int column) try return this.rs.getInt(column); catch(SQLException e) System.out.println(e.toStr
21、ing(); return -1; public boolean rs_next() try return this.rs.next(); catch(SQLException e) System.out.println(e.toString(); return false;
22、; /判斷結(jié)果集中是否有數(shù)據(jù) public boolean hasData() try boolean has_Data = this.rs.first(); this.rs.beforeFirst(); return has_Data; catch(SQLException e) System.out.println(e.toString(); retur
23、n false; public boolean rs_last() try return this.rs.last(); catch(SQLException e) System.out.println(e.toString(); return false; public boolean rs_previous() try return this.rs.previous
24、(); catch(Exception e) System.out.println(e.toString(); return false; /main方法,調(diào)試用 public static void main(String args) try DBConn myconn =
25、 new DBConn(); /myconn.setDBName("shopping"); /myconn.DBConn(); /myconn.execute("Insert Into tbl_test(id,name) values('10','shandaer')"); /myconn.execute("Update tbl_test set name='yyyyyyyyyyyy' where id
26、=10"); /myconn.execute("Delete from tbl_test where id=1"); ResultSet rs = myconn.executeQuery("select * from tbl_user order by id desc limit 1"); /boolean hasData = myconn.hasData(); /System.out.println("has data:" +
27、hasData); /rs.first(); while (myconn.rs.next() int id = myconn.rs_getInt("id") + 1; System.out.print(id); System.out.println(myconn.rs_getInt("id") + myconn.rs_getString("name&q
28、uot;); /System.out.println('n' + myconn.rs_getHtmlString("name"); /System.out.println(myconn.rs.getString("name") + myconn.rs_getInt(1); catch(Exception e) System.err.println(e.toString(
29、); 聲明:因?yàn)槭褂玫氖荕ySQL數(shù)據(jù)庫,所以需要MySQL數(shù)據(jù)庫的驅(qū)動(dòng)下載后請將org包放至DBConn.java所在目錄下以確保該bean能正常運(yùn)行 三、編寫用戶注冊的bean:reg.java/reg.java/import required classesimport java.sql.*;public class reg public int newID = 0; public boolean result = false; public boolean reg(String username,String
30、password,String confirm,String email) try if(!this.checkUser(username) return false; if(!this.checkPwd(password) return false; if(!this.verifyPwd(password,confirm) return false; if(!this
31、.checkEmail(email) return false; if(!this.userNotExit(username) return false; this.getNewID(); this.result = this.register(username,password,confirm,email); return this.result; catch(Exception e)
32、160; System.out.println(e.toString(); return false; /End boolean reg public boolean checkUser(String user) try if(user.indexOf("'")!=-1) System.out.println("姓名中含有非法字符!");
33、60; return false; else return true; catch(Exception e) System.out.println(e.toString(); return false; public boolean checkPwd(String pwd) try if(pwd.indexOf("'"
34、)!=-1) System.out.println("密碼中含有非法字符!"); return false; else return true; catch(Exception e) System.out.println(e.toString(); return false; public boolean ve
35、rifyPwd(String pwd,String confirm) try if(!pwd.equals(confirm) System.out.println("兩次輸入的密碼不一致!"); return false; else return true; catch(Exception e) System.out.println(e
36、.toString(); return false; public boolean checkEmail(String email) try if(email.indexOf("'")!=-1) System.out.println("E-mail中含有非法字符!"); return false; else&
37、#160; return true; catch(Exception e) System.out.println(e.toString(); return false; public boolean userNotExit(String user) try DBConn userDBConn = new DBConn(); userDBConn.executeQuery("sele
38、ct * from tbl_user where name='" + user + "'"); if(userDBConn.rs_next() System.out.println("用戶名已存在,請選擇其它的用戶名!"); return false; else return true; catch(Exception e) &
39、#160; System.out.println(e.toString(); return false; public int getNewID() try DBConn newIDDBConn = new DBConn(); newIDDBConn.executeQuery("select * from tbl_user order by id desc limit 1"); if(newIDD
40、BConn.rs_next() this.newID = newIDDBConn.rs_getInt("id") + 1; System.out.println(this.newID); else this.newID = 1; return this.newID; catch(Exception e) System.out.prin
41、tln(e.toString(); return -1; public int getID() return this.newID; public boolean register(String username,String password,String confirm,String email) try DBConn regDBConn = new DBConn(
42、); String strSQL = "insert into tbl_user(id,name,pwd,email) values('" + this.newID +"','" + username + "','" + password + "','" + email + "')" regDBConn.execute(strSQL); return true;
43、 catch(Exception e) System.out.println(e.toString(); return false; public static void main(String args) try reg newreg = new reg(); System.out.println(newreg.reg("sss
44、ssssss","ssssss","ssssss","imagebear"); DBConn myconn = new DBConn(); myconn.executeQuery("select * from tbl_user"); while(myconn.rs_next() System.out.println(myconn.rs_getInt
45、("id") + " " + myconn.rs_getString("name") + " " + myconn.rs_getString("pwd") + " " + myconn.rs_getString("email"); System.out.println(newreg.getID();&
46、#160; catch(Exception e) System.err.println(e.toString(); 說明:1、該bean文件應(yīng)和上文所述DBConn.class文件放于同一目錄下2、本例主要研究注冊的過程,其中的Email檢測等方法并不完善,若要應(yīng)用請自行設(shè)計(jì)方法 四、編寫用戶登陸的Servlet:login.java/login.java/import required classesimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;
47、import java.sql.*;/class loginpublic class login extends HttpServlet public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException String username = req.getParameter("username"); String password = req.getParameter("pa
48、ssword"); if(this.checklogin(username,password) Cookie mylogin = new Cookie("username",username); mylogin.setVersion(1); mylogin.setPath("/"); mylogin.setComment("Your login username"); res.ad
49、dCookie(mylogin); /Cookie myCookies = req.getCookies(); /String nameValue = this.getCookieValue(myCookies,"username","not found"); /PrintWriter out = res.getWriter(); /out.println("username" + ":" + nameValue); /out.printl
50、n("Test Cookie Success!"); res.sendRedirect("/index.jsp"); public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException doGet(req,res); public static String getCookieValue(Cookie co
51、okies,String cookieName,String defaultValue) for(int i=0;i<cookies.length;i+) Cookie cookie = cookiesi; if (cookieName.equals(cookie.getName() return(cookie.getValue(); return(defaultValue); public boolean checklogin(String username,St
52、ring password) try DBConn loginConn = new DBConn(); loginConn.executeQuery("select * from tbl_user where name='" + username + "'"); if(loginConn.rs_next() System.out.println("Connection cr
53、eated!"); if(loginConn.rs_getString("pwd").trim().equals(password) System.out.println(loginConn.rs_getString("name"); return true; else
54、 return false; System.out.println("Test Login Success!"); return false; catch(Exception e) System.out.println(e.toString(); return false; public stat
55、ic void main(String args) login mylogin = new login(); System.out.println(mylogin.checklogin("shandong","shandong"); 說明:1、默認(rèn)的jdk1.4中并沒有servlet包,請至sun公司網(wǎng)頁下載servlet.jar,放至jdk目錄下的jrelib目錄下,并在JCreator中設(shè)置jdk處添加servlet.jar包 2、本Servlet用于檢驗(yàn)用戶名和密碼,若正確則將用
56、戶名寫入Cookie,完成后將當(dāng)前頁重定向到index.jsp頁 五、編寫檢測用戶是否已經(jīng)登陸的bean:checkLogin.java/checkLogin.java/import required classesimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;/class checkLoginpublic class checkLogin public String username = "" public boolean check(Htt
57、pServletRequest req,HttpServletResponse res) throws IOException,ServletException String cookieName = "username" Cookie myCookies = req.getCookies(); this.username = this.getCookieValue(myCookies,cookieName,"not found"); PrintWriter out = res.get
58、Writer(); if(this.username != null) /out.println("早上好," + this.username + "!"); return true; else out.println("登陸失?。?quot;); return false; public String
59、 getUserName() return this.username; public static String getCookieValue(Cookie cookies,String cookieName,String defaultValue) for(int i=0;i<cookies.length;i+) Cookie cookie = cookiesi; if (cookieName.equals(cookie.getName() return(cooki
60、e.getValue(); return(defaultValue); 說明:此bean檢測cookie中的username,若不為空則說明已登錄,反之說明沒有登錄。方法不夠完善,您可以自行擴(kuò)充。 六、在JRun中建立shopping服務(wù)器打開JRun Administrator,新建shopping服務(wù)器,這里端口為8101。將上文所述所有編譯后的class文件連同org包拷至JRun的shopping服務(wù)器所在目錄中的classes文件夾下,路徑為:C:JRun4serversshoppingdefault-eardefault-warWEB-I
61、NFclasses七、建立jsp文件應(yīng)用DW,在C:JRun4serversshoppingdefault-eardefault-war目錄下新建如下的jsp文件:index.jsp:<% page contentType="text/html;charset=gb2312" pageEncoding="gb2312" %><html><head><title>Shopping123</title><meta http-equiv="Content-Type" cont
62、ent="text/html; charset=gb2312"><link href="styles/shoppingstyle.css" rel="stylesheet" type="text/css"></head><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0"><jsp:useBean id="checklogin" clas
63、s="checkLogin" scope="page"/><% boolean login = checklogin.check(request,response);%><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"> <tr bgcolor="#990000">
64、160; <td height="80" colspan="5"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"> <tr> &
65、#160; <td width="120"> </td> <td class="caption">Shopping123</td> <td width="200"> </td>
66、160; </tr> </table></td> </tr> <tr> <td width="200" align="center" valign="top"><table width="100%" height="20" border="0
67、" cellpadding="0" cellspacing="0"> <tr> <td> </td> </tr> </table>
68、 <% if(!login) %> <table width="90%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <form name="form1"
69、; method="post" action="/servlet/login"> <tr align="center" bgcolor="#CCCCCC"> <td height="30" colspan="2" class="deepred"
70、;>賣場入口</td> </tr> <tr> <td width="50%" height="24" align="center" bgcolor="#FFFFFF">會(huì)員</td&
71、gt; <td align="center" bgcolor="#FFFFFF"><input name="username" type="text" id="username" size="10"></td> </tr>
72、60; <tr> <td height="24" align="center" bgcolor="#FFFFFF">密碼</td> <td align="center" bgcolor="#
73、FFFFFF"><input name="password" type="text" id="password" size="10"></td> </tr> <tr> <t
74、d height="24" align="center" bgcolor="#FFFFFF"><a href="reg.jsp" target="_blank" class="red">注冊</a></td> <td align="center" bgcolor="#FFFFFF"&
75、gt;<input type="submit" name="Submit" value="進(jìn)入"></td> </tr> </form> </table> <% else out.println("
76、;您好," + checklogin.getUserName() + "!"); %> </td> <td width="1" valign="top" bgcolor="#CCCCCC"></td> <td width="400"> </td> <td width="1
77、" valign="top" bgcolor="#CCCCCC"></td> <td width="200"> </td> </tr> <tr align="center" bgcolor="#990000"> <td height="60" colspan="5&quo
78、t; class="white">copyright© 2003 Shopping123</td> </tr></table></body></html>reg.jsp<% page contentType="text/html;charset=gb2312" pageEncoding="gb2312" %><html><head><title>Shopping123</title>
79、;<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link href="styles/shoppingstyle.css" rel="stylesheet" type="text/css"></head><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0"
80、><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"> <tr bgcolor="#990000"> <td height="80" colspan="5"><table width="100%" height=&quo
81、t;100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="120"> </td> &
82、#160; <td class="caption">Shopping123</td> <td width="200"> </td> </tr> </table></td> </tr>
83、 <tr> <td width="100" align="center" valign="top"> </td> <td width="1" valign="top"></td> <td width="400" align="center" valign
84、="top"><table width="100%" height="20" border="0" cellpadding="0" cellspacing="0"> <tr> <td> </td>
85、0; </tr> </table> <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <form action="regpost.
86、jsp" method="post" name="form1"> <tr align="center"> <td height="30" colspan="2" bgcolor="#CCCCCC" class="deepred"&g
87、t;會(huì)員注冊</td> </tr> <tr> <td width="50%" height="24" align="center" bgcolor="#FFFFFF">會(huì)員</td>
88、 <td align="center" bgcolor="#FFFFFF"><input name="username" type="text" id="username" size="16"></td> </tr>
89、 <tr> <td width="50%" height="24" align="center" bgcolor="#FFFFFF">密碼</td> <td align="center" bgcolor="#FFFFFF"><
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 淺談新媒體藝術(shù)創(chuàng)作中動(dòng)畫形象與品牌IP的設(shè)計(jì)研究
- 7月份分級(jí)護(hù)理質(zhì)控
- 市政工程基礎(chǔ)培訓(xùn)教材
- 內(nèi)科期末網(wǎng)課復(fù)習(xí)指南
- 茶樓與餐飲業(yè)聯(lián)營合作協(xié)議范本
- 餐廳服務(wù)員提成合同范本
- 專業(yè)展覽布展設(shè)計(jì)與品牌宣傳合同
- 數(shù)字創(chuàng)意產(chǎn)業(yè)園區(qū)廠房租賃與文化創(chuàng)意合作合同
- 智能化住宅小區(qū)物業(yè)運(yùn)營管理服務(wù)協(xié)議
- 商業(yè)綜合體場地承包經(jīng)營合同規(guī)范
- 2025年高考北京卷化學(xué)高考真題+答案(參考版)
- 2025至2030中國汽車濾清器行業(yè)市場發(fā)展分析及商業(yè)模式與投融資報(bào)告
- 仗鼓舞比賽活動(dòng)方案
- 南昌職業(yè)大學(xué)《影視配音創(chuàng)作》2023-2024學(xué)年第二學(xué)期期末試卷
- 2024年湖南融通資源循環(huán)產(chǎn)業(yè)有限公司技能崗位招聘真題
- 樹木砍伐合同簡單協(xié)議書
- 2025年安徽省農(nóng)業(yè)職業(yè)技能大賽(水生物病害防治員)備賽試題庫(含答案)
- 安全大講堂教學(xué)課件
- 靜電放電(ESD)及其防護(hù)措施培訓(xùn)課件
- 城市更新中歷史文化街區(qū)非物質(zhì)文化遺產(chǎn)保護(hù)與開發(fā)報(bào)告
- 家裝修泥水工合同協(xié)議
評論
0/150
提交評論