lab4Creating a Simple Web Application Using a MySQL Database.docx
- 文档编号:6508959
- 上传时间:2023-01-07
- 格式:DOCX
- 页数:22
- 大小:387.61KB
lab4Creating a Simple Web Application Using a MySQL Database.docx
《lab4Creating a Simple Web Application Using a MySQL Database.docx》由会员分享,可在线阅读,更多相关《lab4Creating a Simple Web Application Using a MySQL Database.docx(22页珍藏版)》请在冰豆网上搜索。
lab4CreatingaSimpleWebApplicationUsingaMySQLDatabase
CreatingaSimpleWebApplicationUsingaMySQLDatabase
ThisdocumentdescribeshowtocreateasimplewebapplicationthatconnectstoaMySQLdatabaseserver.Italsocoverssomebasicideasandtechnologiesinwebdevelopment,suchasJavaServerPages(JSP),JavaServerPagesStandardTagLibrary(JSTL),theJavaDatabaseConnectivity(JDBC)API,andtwo-tier,client-serverarchitecture.ThistutorialisdesignedforbeginnerswhohaveabasicunderstandingofwebdevelopmentandarelookingtoapplytheirknowledgeusingaMySQLdatabase.
MySQLisapopularOpenSourcedatabasemanagementsystemcommonlyusedinwebapplicationsduetoitsspeed,flexibilityandreliability.MySQLemploysSQL,orStructuredQueryLanguage,foraccessingandprocessingdatacontainedindatabases.
ThistutorialcontinuesfromtheConnectingtoaMySQLDatabasetutorialandassumesthatyoualreadyhaveaconnectiontoaMySQLdatabasecreatedandregisteredinNetBeansIDE.Thetabledatathatisincludedinifpwafcad.sql.isalsorequiredforthistutorial.ThisSQLfilecreatestwotables,SubjectandCounselor,thenpopulatesthemwithsampledata.Savethisfiletoalocaldirectory,thenopenitinNetBeansIDEandrunitontheMySQLdatabase.ThedatabaseusedinthistutorialisnamedMyNewDatabase.
Contents
∙PlanningtheStructure
∙CreatingaNewProject
∙PreparingtheInterface
∙SettingupaConnectionPool
oSettingupaJNDIDatasource
oReferencingtheDatasourcefromtheApplication
oAddingtheDatabaseDriver'sJARFiletotheServer
∙AddingDynamicLogic
oAddingtheJSTLLibrarytotheProject'sClasspath
oAddingtaglibDirectivestotheJSPPages
oAddingJSPandJSTLCode
∙DeployingandRunningtheProject
∙SeeAlso
Tofollowthistutorial,youneedthefollowingsoftwareandresources.
SoftwareorResource
VersionRequired
NetBeansIDE
WebandJavaEEinstallation
version6.0
JavaDeveloperKit(JDK)
version6orversion5
MySQLdatabaseserver
version5.x
MySQLConnector/JJDBCDriver
version5.x
GlassFishapplicationserver
or
Tomcatservletcontainer
V2
version6.x
Notes:
∙TheNetBeansIDE6.1withGlassfishandMySQLBundleDownloadprovidesyouwithanall-in-onepackageofsoftwarerequiredforthistutorial.
∙TheWebandJavaEEinstallationenablesyoutooptionallyinstalltheGlassFishV2applicationserverandtheApacheTomcatservletcontainer6.0.x.Youmustinstalloneofthesetoworkthroughthistutorial.
∙TheMySQLConnector/JJDBCDriver,necessaryforcommunicationbetweenJavaplatformsandtheMySQLdatabaseprotocol,isincludedinNetBeansIDE6.x.
∙Ifyouneedtocompareyourprojectwithaworkingsolution,youcandownloadthesampleapplication.
PlanningtheStructure
Simplewebapplicationscanbedesignedusingatwo-tierarchitecture,inwhichtheapplicationcommunicatesdirectlywithadatasourceusingtheJavaDatabaseConnectivityAPI.Auser'srequestsaresenttoadatabase,andtheresultsaresentdirectlybacktotheuser.Two-tierarchitecturescanbeeasilymappedtoaclient-serverconfiguration,whereauser'sbrowserservesastheclient,andaremotedatabasereachableovertheInternetcorrespondstotheserver.
TheapplicationyoubuildinthistutorialinvolvesthecreationoftwoJavaServerPages.Ineachofthesepages,youaddbasicHTMLtoimplementasimpleinterface,followedbySQLtagsprovidedbyJSTLtechnologyinordertoquerythedatabase.Considerthefollowingclient-serverscenario:
Thewelcomepage(index.jsp)presentstheuserwithasimpleHTMLform.Whenaclientrequeststheindex.jsppage,theJSPcodecontainedthereinisparsed,anddatafromtheSubjectdatabasetableisgathered,addedtothepage,andsenttotheclient.TheusermakesaselectionintheprovidedHTMLformandsubmits,whichcausestheclienttomakearequestforresponse.jsp.Whenresponse.jspisparsed,datafromboththeSubjectandCounselortablesisgatheredandinsertedintothepage.Finally,thepageisreturnedtotheclientandtheuserviewsdatabaseduponhisorherselection.
CreatingaNewProject
Inordertoimplementthescenariodescribedabove,youdevelopasimpleapplicationforafictitiousorganizationnamedIFPWAFCAD,orTheInternationalFormerProfessionalWrestlers'AssociationforCounselingandDevelopment.Theapplicationenablesausertochooseacounselingsubjectfromadrop-downlist(index.jsp),thenretrievesdatafromtheMySQLdatabaseandreturnstheinformationtotheuser(response.jsp):
index.jsp
response.jsp
CreateanewprojectintheIDE:
1.StartNetBeansIDEandchooseFile>NewProject(Ctrl-Shift-N)fromthemainmenu.UnderCategoriesselectWeb;underProjectsselectWebApplication.ClickNext.
2.InProjectName,enterIFPWAFCAD.FromtheServerdrop-downlist,selecttheserveryouplanworkwith.LeaveallothersettingsattheirdefaultsandclickFinish.
TheIDEcreatesaprojecttemplatefortheentireapplication,andopensanemptyJSPpage(index.jsp)intheSourceEditor.index.jspservesastheentrypointfortheapplication.ThenewprojectisstructuredaccordingtoSunJavaBluePrintsguidelines.
PreparingtheInterface
Beginbypreparingasimpleinterfaceforthetwopages.Bothindex.jspandresponse.jspimplementanHTMLtabletodisplaydatainastructuredfashion.index.jspalsorequiresanHTMLformthatincludesadrop-downlist.
index.jsp
Makesureindex.jspisopenedintheSourceEditor.Ifitisnotalreadyopen,double-clickindex.jspfromIFPWAFCAD>WebPagesintheProjectswindow.
1.IntheSourceEditor,changethetextbetweenthe
Alsochangethetextbetweenthe
tagstoWelcometoIFPWAFCAD,theInternationalFormerProfessionalWrestlers'AssociationforCounselingandDevelopment!
.
Note:
Forfurthercontent,youcaneitheraddHTMLelementstothepageusingtheIDE'spalette,asdemonstratedbelow,orjustcopyandpastethecodeprovidedinstep8directlyintoyourindex.jsppage.
2.OpentheIDE'sPalettebychoosingWindow>Palette(Ctrl-Shift-8)fromthemainmenu.HoveryourpointerovertheTableiconfromtheHTMLcategoryandnotethatthedefaultcodefortheitemdisplays:
Clicktheicon,anddraganddropatableintothepageintheSourceEditor,toapointjustafterthe
tags.IntheInsertTabledialogthatdisplays,specifythefollowingcriteria,thenclickOK:
oRows:
2
oColumns:
1
oBorderSize:
0
TheHTMLtablecodeisgeneratedandaddedtoyourpage.
3.Addthefollowingcontenttothetableheadingandthecellofthefirsttablerow(newcontentinbold):
4.
5. 6. 7. 8. 9.IFPWAFCADoffersexpertcounselinginawiderangeoffields.
10.
11.
12.
13.professionalwrestlerinyourarea,selectasubjectbelow:
14.
15.Forthebottomrowofthetable,draganddropanHTMLformfromthePaletteintothepage,directlybetweenthesecondpairof
16.Typeinthefollowingtextbetweenthe