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页珍藏版)》请在冰豆网上搜索。
![lab4Creating a Simple Web Application Using a MySQL Database.docx](https://file1.bdocx.com/fileroot1/2023-1/7/9ece20ef-02fb-4443-a2e1-e7a0f30809c4/9ece20ef-02fb-4443-a2e1-e7a0f30809c41.gif)
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
tagstoIFPWAFCADHomepage.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.
IFPWAFCADoffersexpertcounselinginawiderangeoffields. | 8.
---|
9.
10.
11.
12.
ToviewthecontactdetailsofanIFPWAFCADcertifiedformer 13.professionalwrestlerinyourarea,selectasubjectbelow: 14. |
15.Forthebottomrowofthetable,draganddropanHTMLformfromthePaletteintothepage,directlybetweenthesecondpairof
tags.IntheActiontextbox,typeinresponse.jsp,thenclickOK. 16.Typeinthefollowingtextbetweenthe
|