




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、SERVLET&JSP Day05員工管理請求轉(zhuǎn)發(fā)員工管理include指令在MyEclipse中 4.與登錄員工管理請求轉(zhuǎn)發(fā)問題如何使用請求轉(zhuǎn)發(fā)實(shí)現(xiàn)員工的列表顯示。方案重構(gòu)day04的員工管理JSP實(shí)現(xiàn)由Servlet負(fù)責(zé)處理數(shù)據(jù)庫操作,JSP負(fù)責(zé)顯示 1)新建項(xiàng)目web05將web04_jsp下的src目錄、WebRoot目錄及之下所有內(nèi)容拷貝到web05項(xiàng)目下請web04_jsp.zip2)導(dǎo)入數(shù)據(jù)庫驅(qū)動的Jar包3)拷貝項(xiàng)目web04_2中的web.ActionServlet和web.xml到web05項(xiàng)目中請web04_2.zip查詢雇員信息4)修改ActionServlet的內(nèi)容因
2、為Servlet不擅長生成頁面,所以轉(zhuǎn)發(fā)給JSP來處理頁面5)修改emplist.jsp6)完成員工的查詢!完成。1.3 實(shí)現(xiàn)ActionServlet.java代碼如下:01.publicextendsHtpServlet02.03.04.05.06.07.08.09.10.1.publicvoidserviceServletException.setCharacterEncoding= request= uri.lastIndexOf.equals(HtpServletRequestrequest, HtpServletResponsthrows, IOExceptionrequest S
3、tringuri Stringactionuri(utf-8(););.getRequestURI.substring(/ ), uri).lastIndexOf(. );if (action(/list/查詢數(shù)據(jù)庫=.7.38.39.);Listemployes/將/step1綁訂requestemployes./step2獲得轉(zhuǎn)發(fā)器RequestDispatcherdrequest.getRequestDispatcheremplist3.jsp
4、/step3轉(zhuǎn)發(fā).forwardrd(request, response) catch(Exceptionee.thrownew ServletExceptionelseif (action.equals= requestStringnameSystem.outsalary.double= Double.getParameter.parseDouble(request(salary);age=.(request.getParameter(age);try=40.().
5、.9.getInstance(= new(name ); (salary(age ););();Employe e eeEmploye.setName.setSalary.setAge);response catch(Exceptione/step1記日志./step2如果是系統(tǒng)異常,要提示用戶。ethrownewServletException(e);elseif (action id.equals(/del)(requestlongtry= Long.parseLong.getParameter(id
6、);=)(.getInstance(id );.sendRedirect);.deleteresponse(list.do); catch(Exceptione) ();e.thrownewServletException(e);elseif (actionid.equals(/load)long= Long.parseLong.getParameter=)(request(id);(.getInstancetry();Employe requestrequest=.findById(id );, e );(updateEmp.jsp);.getRequestDispatcher (reque
7、st(e).forward, response catch(Exceptione) ();e. 0. 1. 2. 3. 4. 5. 6. . . 5.96.9 .9 .005.thrownew6ervlet(xception(e);elseif(action .eTuals ( /update)id = /ong .parse/ong(reTuest=reTuest .get3arametersalary = ouble .parseoublelong 6tringname double.get3arameter(
8、id );( name );(reTuest .get3arameter.get3arameter( age ( salarage = ,nteger.parse,nt (reTuest);try(mploye$2=) $2)actory( (mploye$2);(mploye$2.get,nstance(mployeee .set,de .set1amee .set6alarye .set$ge.update= new(mploye (id );(name );(salary );(age );(e);();response.send5edirectcatch (xceptionee . t
9、ack7race thrownew6ervlet(xception( list.do);)();(e);emplist.jsp代碼如下:01.02.03.04.05.06.0 .0 .import = Mava.util.*,entity.* % pageemplist / titlemetahtp-eTuiv = &ontent-7ype content = text/html;charset=87)- href = cs/style.cs /type = text/cs 07.28.29
10、..6.47.=top_contentdivid=headerdivid=rightheader209br/ 1 /20/mainh1id=titleahref=navigation=#/ divdivid/ div歡迎%Listemployes=(List) request.(employes);%trclas=table_headerID薪水.3.74.7
11、4.85.86.操作Employe%=%=%=%=ahref  e.getAge()%=; ahref=/ table=添加雇員onclick=location.href=adEmp.jsp . . 5.divid= foterBbg $%& 126 .com1.4 擴(kuò)展員工管理include指令問題如何使用include指令實(shí)現(xiàn)頁面的拆分。方案對于頁面的公共部分,代 碼的優(yōu)化??梢允褂孟嗤膉sp文件,并使用include指令導(dǎo)入,如此可以實(shí)現(xiàn)比如emplist.jsp和addEmp.jsp的“頭
12、部”和“尾部”相同1)emplist.jsp2)addEmp.jsp2.3 實(shí)現(xiàn)演示 1)修改emplist.jsp刪除注釋部分,加入語句2)新建head.jsp3)同理修改addEmp.jsp頁面及其他頁面的,實(shí)現(xiàn)代碼的優(yōu)化。2.4 擴(kuò)展在MyEclipse中問題如何在MyEclipse中方案1)Window選項(xiàng)卡選擇Open數(shù)據(jù)庫。選擇MyEclipse Database Explorer,打開數(shù)據(jù)庫視圖2)在DB Browser中單擊右鍵,選擇New .3)在Database Driver界面配置Driver template: 連接的數(shù)據(jù)庫模板 Driver name: 自定義的名字
13、Connection URL: URLUser name: 用戶名Passwd:【Add JARs】: 添加的Jar包【Test Driver】: 測試一下驅(qū)動(可選)【Save Passwd】:保存下次更方便4)點(diǎn)擊【Finish】后會出現(xiàn)配置好的數(shù)據(jù)庫連接“”5)在新建的數(shù)據(jù)庫連接上點(diǎn)擊右鍵,選擇open connection.6)展開可以看到數(shù)據(jù)庫及表結(jié)構(gòu)7)在”Connected to“上點(diǎn)擊右鍵,選擇”new SQL Editor“8)在出現(xiàn)的”SQL Editor“中輸入SQL語句,點(diǎn)擊”綠色角“執(zhí)行9)顯示結(jié)果44.1 問題與登錄將表單中的用戶數(shù)據(jù)到數(shù)據(jù)庫。在之前,要查詢用戶名是
14、否已經(jīng)存在,如果存在,則跳轉(zhuǎn)到4.2 方案頁面,提示用戶“用戶名已經(jīng)存在,重新輸入。成功,跳轉(zhuǎn)到登錄頁面。步驟說明step1 建表01.02.03.04.05.06.createtableusername name pwdgendarvarchar(50 ) unique(50 ),(20 ),(1 );,varcharvarcharcharstep2 創(chuàng)建一個web工程a.User實(shí)體類b.類c.regist.jspd.ActionServlet4.3 實(shí)現(xiàn)1)建數(shù)據(jù)庫表a.新建文件script.sqlb.切換到MyEclipse Database Explorer視圖中打開數(shù)據(jù)庫連接c.執(zhí)
15、行SQLd.小技巧將要執(zhí)行的代碼“選中”后,點(diǎn)擊右鍵,則只執(zhí)行選中部分的代碼e.script.sql01.02.03.04.05.06.07.08.09.10.droptablet_user;createtablet_user(,username name pwdgendarvarchar(50 ) unique(50 ),(20 ),(1 );,varcharvarcharcharselect* fromt_user;2)新建實(shí)體類entity.User01.02.03.04.05.06.07.08.09.10.1.packageentity;public;p private private
16、 private private publicpublicidStringusername Stringname Stringpwd StringendarUser;() User(Stringusername, Stringname,.9.50.Stringpwd, Stringendar= username= name;)this this thisthis.username.
17、name.pwd.gendar;= pwd;= gendar;publicStringgetGendar()returngendar;publicvoid.gendarsetGendar(Stringendar)this= gendar;publiclonggetId;()returnidpublicvoid.idsetId= id(longid)this;publicStringgetName;()setName(Stringname;) this= namepublicStringgetPwd;()returnpwdpublicvoid.p
18、wdsetPwd= pwd(Stringpwd;) thispublicStringgetUsername()returnusername;publicvoid.usernamesetUsername(Stringusername) this= username;3)拷貝util.DBUtil到項(xiàng)目下01.02.03.04.05.06.07.08.0.34.35.packageutil;import import import/*javajava java
19、.sql.sql.sql.Conection.DriverManager.SQLException;*jdbc工具類,提供獲得連接、關(guān)閉連接的方法* author*/ publicpublicthrowsteacherclasDBUtilConectiongetConection()ExceptionConectioncon= nul;tryClascon.forName= DriverManager();.getConection(+,useUnicode=true&characterEncoding=utf8 rot(Exceptione,1234);) (); catche.throwe
20、 ;returncon;publicvoid!= nul).closeclose(Conectioncon)if (contrycon(); catch(SQLExceptione) e.();5.46./*/paramthrowsargsExceptionpublicvoidmain(String args) throwsException/TODOAuto-generatedmethodstubSystem.out .(getConection();4)新建.User01.02.03.04.05.06.07.08.02.
21、.23.package;import importimportjava javajava.sql.sql.sql.Conection.ResultSet;importimportutilentity.DBUtil.User;public(Useruserpublicvoidsave)throwsExceptionConectioncon= DBUtil.getConection();=con.(+username,name,pwd,gendar)values(?,?,?,?)+);prep prep prep prep prepDBUti
22、l.setString.setString.setString.setString.executeUpdate.close(1, user (2, user (3, user (4, user();(con);.getUsername.getName.getPwd.getGendar();();();();.6.47.48.publicthrowsUserExceptionfindByUsername(Stringusername)nulUseruserConecti
23、oncon=;= DBUtil.getConection();=con.select* fromt_userwhereusername=?+);prepResultSetrst.setString(1, username= prep);.executeQuery();if(rst.next()newuser user user user useruser=.setName.setUsername.setGendar.setPwd.setIdUser(rst();.getString (username(name);(gendar(pwd););(rst .getString(rst .getS
24、tring););(rst.getLong(idDBUtil.closeuser(con;);return5)新建Test01.02.03.04.05.06.07.08.09.packagetest;importimport.;entity.Userpublicclaspublicthrowsvoidmain(String args)Exception/TODOAuto-generatedmethodstub6.17.18.= new();Useruser=new.saveUser(user(zs);,zhangshan,test,m );Useruser2
25、System=.out .findByUsername(zs);6)測試運(yùn)行Test7)拷貝css、img目錄、reg請參照 day04資料8)將 頁面regist.html修改為regist.jsp01.02.03.04.05.06.07.08.0.34.35.36.titleregist-equiv=text/html;charset=UTF-8=styleshet=cs/style.cs=Content-Typecontenthrefdivid=wr
26、apdivid=headerdivid=rightheader209 / 1 /20h1id =titleahrefdivid=navigationdivid=content =whereami=# main method=0=0=0tablecelpadingcelspacingborder.4.75.clas=form_table=midlealign=right用戶名:tdv
27、aligntype=text=inputgri=usernameclasname/spanstyle%=(/ span=color:red;font-style:italic;= nul? : msgmsg真實(shí)tdvalignclas=inputgri=namename/:tdvalign=paswordclas=inputgri=pwdname/.0003.14.男na
28、me value checked女=checkedinputype=radioclas=inputgri=namevalue/ trimgidonclick=.src=image?+(newDate().getTime()換一張atdvalign=textclas=inputgri=numbername/value/ formdivid =foterABC126 .com9)新建web.ActionServlet寫邏輯01.02.03.04.05.06.07.08.08.19.packageweb;import import import i
29、mportimportjava java java javajava.io .IOException.sql .Conection.sql .sql .ResultSet.sql .;import import importimportjavax javax javaxjavax.servlet.servlet.servlet.servlet.ServletException.htp.htp.htp;.HtpServlet.HtpServletRequest.HtpServletResponse;import importimportutil.DBUtil.User;entitypublice
30、xtendsHtpServlet.7.58.publicvoidservice(HtpServletRequestrequestHtpServletResponseresponse,)throwsServletException, IOExceptionrequest Stringuri Stringpathuri.setCharacterEncoding(utf-8);= requ
31、est= uri.lastIndexOf.getRequestURI();.substring(uri);.lastIndexOf(/ ),(.)if(path.equals(/registStringusernamerequest=.getParameter=(username(););newtryUseruser=if(user=nul)/用戶名不存在,user user user useruser=.setName.setPwd.setGendar.setUsername.savenewUser(request (request();.getParameter.getParameter.
32、getParameter(name(pwd(gendar););(request(username);););(user.sendRedirectresponse(main.jsp);else/用戶名存在,則提示用戶request.(regist_eror,用戶名已經(jīng)存在 );request.getRequestDispatcher.forward(regist.jsp)(request, response);catch(Exceptione) ();e.thrownewServletException(e);59.10)web.xml11)新建main.jsp用于測試,成功后直接重定向到ma
33、in.jsp12)提問在重定向代碼后寫輸出語句會不會執(zhí)行?會執(zhí)行。JVM將順序執(zhí)行代碼,除非遇到return或System.exit(); 13)測試a.成功則跳轉(zhuǎn)到主頁面b.重名,則出現(xiàn)紅色提示“用戶名已存在”14)拷貝login.html到項(xiàng)目下,并修改為login.jsp01.02.03.04.05.06.07.08.0.0.41.42.titlelogin=Content-Typecontentdivid=to
34、p_content=headerdivid=rightheader209br/ 1 /20/=# mainh1id=titleahref=navigation/ divdivid/ div=content=whereami登錄=0 border=0clas=form_table用戶名:..1.tdvalign=text=inputgriname=usernamespanstyle
35、=color:red;=%=(msgnul?/ spantdvalign=inputgri=pasword/name=pwd/ tableclas=butonvalue/formABC.com div /body82.15)修改ActionServlet增加的代碼01.02.03.04.05.06.07.08.00.31.32.packageweb;import import import importimportjava java java javajava.io .IO
36、Exception.sql .Conection.sql .sql .ResultSet.sql .;import import importimportjavax javax javaxjavax.servlet.servlet.servlet.servlet.ServletException.htp.htp.htp;.HtpServlet.HtpServletRequest.HtpServletResponse;import importimportutil.DBUtil.User;entitypublicextendsHtpServletpublicvoidservice(HtpServ
37、letRequestrequestHtpServletResponseresponse,)throwsServletException, IOExceptionrequest Stringuri Stringpathuri.setCharacterEncoding(utf-8);= request= uri.lastIndexOf.getRequestURI();(uri .lastIndexOf(/ ),(. );).substringif (path.equals(/registStringusernamerequest=.getParameter= new(username(););3.
38、tryUseruser=.findByUsername(username);if (user=nul/用戶名不存user= newUseruser.setName(requestuser.setPwd(requestuser.setGendar(requestuser.setUsername(usernameresponse.sendRedirect(main.jsp4.else/用戶名存在,則提示用戶request.用戶名已經(jīng)存在 );request.getRequestDispatcher(regist.jsp.forward(request, response);51. catch(Ex
39、ceptione) e.thrownewServletException5.elseif (path.equals(/loginStringusername= requestStringpwd= request.tryUseruser=if (user!= nul& user.getPwd(). equals(pwd )/登錄成功response.sendRedirect(main.jsp);else6./登錄失敗67.request.(login_eror,用戶名或者錯誤 );request.getRequestDispatcher(login.jsp).forward(request, r
40、esponse);.78.79. catch(Exceptione) ();e.thrownewServletException(e);重新部署測試a.成功,則跳轉(zhuǎn)到main.jspb.失敗,則顯示“用戶名或錯誤”18)演示SQL注射(SQL注入)登錄使用S ement會有較大的安全問題:SQL注入 a.修改ActionServlet01.02.03.04.05.06.07.08.01.2.packageweb;import import import importimportjava
41、java java javajava.io .IOException;.sql.sql.sql.sql.Conection.ResultSet.;import import importimportjavax javax javaxjavax.servlet.servlet.servlet.servlet.ServletException.htp.htp.htp;.HtpServlet.HtpServletRequest.HtpServletResponse;import importimportutil.DBUtil.User;entitypublicextendsHtpServletpublicvoidservice(HtpServletRequestrequestHtpServletResponseresponse,).0.61.throwsServletException, IOExceptionrequest Stringuri Stringpathuri.setCharacterEnc
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 激光成品檢驗(yàn)試題及答案分享
- 牙體牙髓病試題及答案
- 系統(tǒng)架構(gòu)設(shè)計(jì)師考試中的數(shù)據(jù)結(jié)構(gòu)解析試題及答案
- 激光設(shè)備生產(chǎn)流程優(yōu)化試題及答案
- 結(jié)果導(dǎo)向稅務(wù)師考試試題及答案
- 現(xiàn)代衛(wèi)生管理證書考核試題及答案
- 自考建筑制圖試題及答案
- 菊花臺考試題及答案
- 聯(lián)合工會筆試題庫及答案
- 醫(yī)學(xué)考研 試題及答案
- 華為培訓(xùn)教程01網(wǎng)絡(luò)基礎(chǔ)
- 管道完整性管理基礎(chǔ)知識課件
- 文體中心運(yùn)營方案
- 宮頸癌防控知識
- 知識產(chǎn)權(quán)與人工智能
- 教師資格證《小池》說課夏東
- 接觸網(wǎng)施工-接觸網(wǎng)竣工驗(yàn)收
- 黑龍江省哈爾濱市香坊區(qū)2023-2024學(xué)年八年級上學(xué)期期末數(shù)學(xué)試題
- GB/Z 43281-2023即時檢驗(yàn)(POCT)設(shè)備監(jiān)督員和操作員指南
- 主動披露報(bào)告表
- 煤礦一通三防知識培訓(xùn)課件
評論
0/150
提交評論