tts8.0課件課后練習(xí)代碼經(jīng)典案例day_第1頁(yè)
tts8.0課件課后練習(xí)代碼經(jīng)典案例day_第2頁(yè)
tts8.0課件課后練習(xí)代碼經(jīng)典案例day_第3頁(yè)
tts8.0課件課后練習(xí)代碼經(jīng)典案例day_第4頁(yè)
tts8.0課件課后練習(xí)代碼經(jīng)典案例day_第5頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余44頁(yè)可下載查看

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Java SERVLET&JSP 2. 購(gòu)物車(提高題選做11.1 問(wèn)通過(guò)Servlet生成一返回給瀏覽器是一張壓縮為jpg格式的隨Java SERVLET&JSP 2. 購(gòu)物車(提高題選做11.1 問(wèn)通過(guò)Servlet生成一返回給瀏覽器是一張壓縮為jpg格式的隨機(jī))1.2 方小知如何寫(xiě)Java程web應(yīng)用Apache提供了一個(gè)工具類http-nt.jar,封裝了http協(xié)議,它提供一些接口及操作可web應(yīng)用。可到圖示演。服務(wù)器部署某個(gè)應(yīng)用,該應(yīng)用添加了驗(yàn)證,必。1)瀏覽器向login.jsp2) jsp會(huì)創(chuàng)建一個(gè)對(duì)3) Jsp返回給瀏覽器一個(gè)登陸頁(yè)面(html、js、該頁(yè)面包含一行代碼(ch

2、eckCode是一個(gè)請(qǐng)求地址4)瀏覽器立即給服務(wù)器發(fā)請(qǐng)求到CheckCodeServlet會(huì)生成一個(gè)隨機(jī)數(shù)(驗(yàn)證數(shù)字)同時(shí),CheckCodeServlet此時(shí),用戶在瀏覽器上看到完全的登錄頁(yè)面象上7)用戶填寫(xiě)完表單后提交8) LoginServlet從對(duì)象中取到驗(yàn)證數(shù)字8) LoginServlet從對(duì)象中取到驗(yàn)證數(shù)字9) LoginServlet將number1與用戶提交請(qǐng)求中的驗(yàn)證數(shù)字number2比較,如果匹配,則繼正常的登錄驗(yàn)證,不匹配則返回給瀏覽器信息不匹配案例演img1)新建2)新建4)部署項(xiàng)服務(wù)器立即打印4)部署項(xiàng)服務(wù)器立即打印說(shuō)明瀏覽checkcode請(qǐng)1.3頁(yè)面test.h

3、tml時(shí),在執(zhí)行語(yǔ)句時(shí)向服務(wù)器發(fā)送CheckcodeServlet代碼如下package importimport importimportimportimportimport import importimportimportimportimportimport import importimportimportimportpublicclass CheckcodeServlet extends HttpServlet public void service(HttpServletRequestHttpServletResponsethrows ServletException, /* 1

4、設(shè)置服務(wù)器返回文件的格式為jpg格式/* 2 畫(huà)圖 BufferedImage image =new BufferedImage(60,Random r = new Random();/獲得畫(huà)Graphics g = /設(shè)置背景顏g.setColor(new /填充背景g.fillRect(0, 0, 60, /設(shè)置前景顏g.setColor(new /生成隨String number = /畫(huà)上g.drawStringg.setColor(new /生成隨String number = /畫(huà)上g.drawString(number, 5, /* 3并輸出 /獲得字節(jié)輸出流,因?yàn)橐敵龅氖菆D像

5、壓縮/的字節(jié)數(shù)組,所以,不能用WriterOutputStreamos=/壓縮,輸JPEGImageEncoderencoder隱web.xml代碼如下?xml web-app=1.0 encoding=UTF-h/xml/ns/j2ee/web-CheckcodeServlet/servlet-web.CheckcodeServletCheckcodeServlet/servlet-/checkcode/url-/web-隱test.html代碼如下meta httptest.html代碼如下meta http-equiv=Content-content=text/html; charset

6、=UTF-Insert title img 隱每次刷新會(huì)生成一個(gè)新的隨增加更修改功1.4頁(yè)面加。驗(yàn)證案拷貝day06的修改1.4頁(yè)面加。驗(yàn)證案拷貝day06的修改添%page pageEncoding=utf-8 meta http-equiv=Content-Type content=text/html; charset=UTF-link rel=stylesheet type=text/css href=css/style.css div div div div div div 2009/11/20br div h1 a div div form action=regist.do tabl

7、ecellpadding=0cellspacing=0border=0 tdvalign=middle用戶名td valign=middle inputtype=textname=usernameString msg = span style=color:red;font-%=(msg = null ? : tdvalign=middle真tdvalign=middle真:td valign=middle input type=text class=inputgri name=name tdvalign=middle:td valign=middle input type=password c

8、lass=inputgri tdvalign=middle:valign=middle男女tdvalign=middle:img id=img1 src=checkcode 換一張td valign=middle 換一張td valign=middle input type=text name=number input type=submit value=確認(rèn) div div 隱3.拷貝CheckedServlet到項(xiàng)目package importimport import import importimportimportimportimportimportimport;importimpo

9、rtimportimportimport;importimportpublicclass CheckcodeServlet extends HttpServlet public void service(HttpServletRequest HttpServletResponse throws ServletException,BufferedImage image =new BufferedImage(60,Random r = new /獲得畫(huà)Graphicsg=image.getGraphics(); new (255),g.fillRect(0, 0, 60, 20);g.setCol

10、or(newStringnumber=g.drawString(number, 5, /獲得字節(jié)輸出流,因?yàn)橐敵龅氖菆D像壓縮/的字節(jié)數(shù)組,所以,不能用WriterOutputStreamos=/壓縮,輸JPEGImageEncoder encoder = 隱4.修改部署隱4.修改部署應(yīng)測(cè)點(diǎn)擊“換一張”能驗(yàn)7.修改綁定生成的驗(yàn)證數(shù)字到注意:給用驗(yàn)7.修改綁定生成的驗(yàn)證數(shù)字到注意:給用戶看的+驗(yàn)證數(shù)字;放入對(duì)象的是驗(yàn)證數(shù)8.修改添加“時(shí)比較用戶輸入驗(yàn)證數(shù)字和中取出的驗(yàn)證數(shù)字”的代package import importimportimport importimport import impor

11、t importimportimportimportimport importimport import import importimport;importimport import;publicionServlet extends public void service(HttpServletRequestrequest, HttpServletResponse response)throws ServletException, IOException request.setCharacterEncoding(utf-uri=request.getRequestURI(); path =

12、uri.substring(uri.lastIndexOf(/), (path.equals(/regist) /* 1 先比/1.1 表單提交String number1 = /1.2 中綁訂=Stringnumber2(String) /用于測(cè)+ /當(dāng)用戶禁將失效,number2為(number2 != null number2.equals(number1) usernametry = new Useruser (number2 != null number2.equals(number1) usernametry = new Useruser (user = null) 戶名不存在操u

13、ser = new User(); else / 用戶名存在,則提示tribute(regist_error, 用戶名已經(jīng)存在equest,rcatch (Exception e) throw new else 錯(cuò)錯(cuò)誤 else (path.equals(/login) username=pwd= new try User user(user != null & user.getPwd().equals(pwd) 錄成/ 向中try User user(user != null & user.getPwd().equals(pwd) 錄成/ 向中綁訂= Id:+tribute(user,els

14、e 錄失tribute(login_error, 用戶名或錯(cuò) catch (Exception e) throw new else if (path.equals(/logout) = 隱9.修改加入驗(yàn)證失敗的%page pageEncoding=utf-8 http-equiv=Content-Type content=text/html; charset=UTF-rel=stylesheet type=text/css href=css/style.css http-equiv=Content-Type content=text/html; charset=UTF-rel=styleshe

15、et type=text/css href=css/style.css div div div 2009/11/20br div h1 a div div form action=regist.do tablecellpadding=0cellspacing=0border=0 tdvalign=middle用戶名td valign=middle inputtype=textname=usernameString msg span style=color:red;font-%=(msg = null span style=color:red;font-%=(msg = null ? : tdv

16、align=middle真:td valign=middle input type=text tdvalign=middle:td valign=middle input type=password class=inputgri tdvalign=middle:tdvalign=middle男inputtype=radioname=gendarvalue=m女td valign=middle :img id=img1 src=checkcode a :img id=img1 src=checkcode 換一張td valign=middle input type=text name=numbe

17、r String msg2 = spanstyle=color:red;font-%=(msg2 = null ? /div div 隱重新部測(cè)成功,跳入登錄成功,跳入登錄加干擾線或干擾點(diǎn)(防止機(jī)器人識(shí)別13.修改購(gòu)物車(提高題選做問(wèn)購(gòu)物車的購(gòu)物車(提高題選做問(wèn)購(gòu)物車的主要功能商品列刪除購(gòu)物車當(dāng)中的商修改購(gòu)物車當(dāng)中的商品的數(shù)先需要實(shí)現(xiàn)刪除購(gòu)物車當(dāng)中的商修改購(gòu)物車當(dāng)中的商品的數(shù)先需要實(shí)現(xiàn)商品列表2.2商品功puter(id primarypprodDesc price double);ovalues(x200,x200.jpg,還算便宜,2000);ovalues(x500,x500.jpg,性

18、價(jià)比最好,4000);ovalues(x600,x600.jpg,性能不錯(cuò),6000);step3 Computer實(shí)體step4step5Computer step6 ActionServlet類step7項(xiàng)目結(jié)建表&準(zhǔn)備建表&準(zhǔn)備數(shù)新建web工程新建3.啟動(dòng)MyEclipse數(shù)據(jù)庫(kù)連接&執(zhí)行數(shù)據(jù)3.啟動(dòng)MyEclipse數(shù)據(jù)庫(kù)連接&執(zhí)行數(shù)據(jù)之前配數(shù)據(jù)庫(kù),請(qǐng)參考servlet第2天課堂筆注意配置數(shù)據(jù)庫(kù)連接時(shí)設(shè)置編a啟動(dòng)數(shù)據(jù)庫(kù)b.執(zhí)行數(shù)據(jù)實(shí)體類操作根據(jù)數(shù)據(jù)庫(kù)表新建實(shí)體package public 實(shí)體類操作根據(jù)數(shù)據(jù)庫(kù)表新建實(shí)體package public class Computer long

19、 private privateprivateprivateprivatedouble public Computer() public Computer(String,String pic, String prodDesc, double =this.pic = dDesc = this.price = public public publicpubliclong getId() return void setId(long id) dDesc = this.price = public public publicpubliclong getId() return void setId(lo

20、ng id) this.id = Stringvoid ()return (String ) = String getPic() returnvoid setPic(String public public public public publicpublicthis.pic = double getPrice() return void setPrice(double price) this.price= price; String getProdDesc() return prodDesc;void setProdDesc(String =隱導(dǎo)入jar拷貝package import im

21、port import* jdbc工具類,提供獲得連接、關(guān)閉連接的* author *public class DBUtil public ic Connection throws Exception Connection conn= null; try conn=+catch (Exception e) throw e;return public ic catch (Exception e) throw e;return public ic void try; catch (SQLException e) public ic void main(String throws Exception

22、/TODOAuto-generatedmethod隱新方package;import import import importimportimportimportpublic class public ListfindAll()throwsExceptionListcomputers= new ArrayList();Connectionconnimportpublic class public ListfindAll()throwsExceptionListcomputers= new ArrayList();Connectionconnementprepselect*from Result

23、Set rst =Computer c =new return public ComputerfindById(longid)throwsExceptionComputerc=null;Connectionconnementprepselect*from prep.setLong(1, id); ResultSet rst =puterwherec = new c = new return c;隱測(cè)新建測(cè)試很重要7.拷貝css&html請(qǐng)拷貝computer_list.html到項(xiàng)目下,改名為新建添加查package 拷貝computer_list.html到項(xiàng)目下,改名為新建添加查packa

24、ge importimportimport import import importimport;import import importimport;publicionServlet extends public void service(HttpServletRequest HttpServletResponsethrows ServletException,Stringuri=request.getRequestURI(); String path =HttpServletResponsethrows ServletException,Stringuri=request.getReque

25、stURI(); String path =new try Listcomputerstribute(computers,.forward(request, catch (Exception e) throw new 隱?xml web-app=1.0 encoding=UTF-h/xml/ns/j2ee/web-ActionServlet/servlet-web.ActionServlet/servlet-ActionServlet/servlet-*.do/url-/servletActionServlet/servlet-*.do/url-/servlet-隱修%page pageEnc

26、oding=utf-8 contentType=text/html;charset=utf-%page import=java.util.*,entity.* /meta http-equiv=Content-Type content=text/html; charset=utf-body div table cellSpacing=6 cellPadding=2 width=100% 主頁(yè);href=#筆記本訂購(gòu)&;商品列br tableordercellPadding=0width=100%tdbr tableordercellPadding=0width=100%td 型號(hào)td 產(chǎn)td

27、產(chǎn)品說(shuō)明td 產(chǎn)品報(bào)價(jià)td Listcomputers= for(Computer c = td&td imgsrc=img/d007/height=90td td td td a br value=name=settingsubmit隱部署應(yīng)測(cè)查看功能實(shí)現(xiàn)實(shí)現(xiàn)圖示演示不同用商品數(shù)據(jù)存放在不同查看功能實(shí)現(xiàn)實(shí)現(xiàn)圖示演示不同用商品數(shù)據(jù)存放在不同中圖示演示流程不再直的方法,而是綁定一個(gè)Cart對(duì)用商品時(shí),只調(diào)用Cart的add方法進(jìn)行操作,而不再直接操作刪除(修改)時(shí)也一如此以后只要操作Cart類的方法就OK了,不再直接操作。這是一個(gè)經(jīng)典的計(jì)Cart和CartItem偽實(shí)現(xiàn)步14.新建商品條目類,作

28、用是Cart和CartItem偽實(shí)現(xiàn)步14.新建商品條目類,作用是為了更方便實(shí)現(xiàn)購(gòu)物車Cart的實(shí)package import*author publicclass privatec;privatepublic Computer getC() return void setC(Computer c) this.c = getQty() return qty;void qty) public Computer getC() return void setC(Computer c) this.c = getQty() return qty;void qty) publicpublic public

29、=隱15.新建package import import *購(gòu)物實(shí)現(xiàn)了商品的添加、刪除、列表、清空、author public class Cart /items的所有商privanewist items publicadd(CartItem /先判斷有沒(méi)過(guò)該商品,如果買/則返回false,否則,添加到itemsfor(CartItem curr = items.get(i); if(curr.getC().getId() = item.getC().getId()return return true;public List list()return items;public void id)

30、for(public List list()return items;public void id)for(CartItemcurr=if(curr.getC().getId() = id) return;public clear()items.cle public void modify(long qty)for(CartItem curr = items.get(i); if(curr.getC().getId() = id)return;public doubletotal()double total= for(i=0;iitems.size();i+)CartItem curr = i

31、tems.get(i);total += curr.getQty() * 隱修添的地17.修改實(shí)功package importimport隱修添的地17.修改實(shí)功package importimportimportimportimportimport import;importimportimport import;publicionServlet extends public void service(HttpServletRequestHttpServletResponsethrows ServletException, IOException Stringuri=Stringpathif

32、(path.equals(/list) Stringuri=Stringpathif(path.equals(/list) =newtry Listcomputerstribute(computers,.forward(request, catch (Exception e) throw new else long id = =new Computer c = CartItemitem = new =Cartcart=(Cart)sesif(cart=null)/如果是第一/然后綁訂到,需要先創(chuàng)建好cart對(duì)cart = new tribute(cart, flag=/已過(guò)該商品,則提示tribute(buy_error + .forward(request, else.forward(request, elsecatch (Exception e) thro

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論