计算机外文翻译 XML与JSP联手.docx

上传人:b****7 文档编号:16403297 上传时间:2023-07-13 格式:DOCX 页数:14 大小:26.18KB
下载 相关 举报
计算机外文翻译 XML与JSP联手.docx_第1页
第1页 / 共14页
计算机外文翻译 XML与JSP联手.docx_第2页
第2页 / 共14页
计算机外文翻译 XML与JSP联手.docx_第3页
第3页 / 共14页
计算机外文翻译 XML与JSP联手.docx_第4页
第4页 / 共14页
计算机外文翻译 XML与JSP联手.docx_第5页
第5页 / 共14页
计算机外文翻译 XML与JSP联手.docx_第6页
第6页 / 共14页
计算机外文翻译 XML与JSP联手.docx_第7页
第7页 / 共14页
计算机外文翻译 XML与JSP联手.docx_第8页
第8页 / 共14页
计算机外文翻译 XML与JSP联手.docx_第9页
第9页 / 共14页
计算机外文翻译 XML与JSP联手.docx_第10页
第10页 / 共14页
计算机外文翻译 XML与JSP联手.docx_第11页
第11页 / 共14页
计算机外文翻译 XML与JSP联手.docx_第12页
第12页 / 共14页
计算机外文翻译 XML与JSP联手.docx_第13页
第13页 / 共14页
计算机外文翻译 XML与JSP联手.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

计算机外文翻译 XML与JSP联手.docx

《计算机外文翻译 XML与JSP联手.docx》由会员分享,可在线阅读,更多相关《计算机外文翻译 XML与JSP联手.docx(14页珍藏版)》请在冰点文库上搜索。

计算机外文翻译 XML与JSP联手.docx

计算机外文翻译XML与JSP联手

英文原文:

UsingXMLandJSPtogether

I'mgoingtoassumethat,likemostJavaprogrammers,youknowwhatJavaServerPages(JSP)andExtensibleMarkupLanguage(XML)are,butyoumaybealittleunclearonhowyoucanusethem.JSPuseisprettyeasytodefend.ItallowsyoutodesignaWebsitebuiltfromfilesthatlookandactalotlikeHTML.TheonlydifferenceisthatJSPsalsoactdynamically--forexample,theycanprocessformsorreaddatabases--usingJavaasaserver-sidescriptinglanguage.XMLuseismoredifficulttojustify.Whileitseemsasifeverynewproductsupportsit,eachoneseemstobeusingXMLforadifferentpurpose.

Inthisarticle,youwilllearntodesignasystemusingXMLinafairlymodestway.ManyWebsiteshavevastcollectionsofdatathataredisplayedinamoreorlessstandardway.IwilldesignasystemthatusesXMLfilestostoredataonaWebserverandJSPfilestodisplaythatdata.

XMLversusrelationaldatabases

"Butwait,"youmayask,"you'reusingXMLtostoredata?

Whynotuseadatabase?

"Goodquestion.Theansweristhatformanypurposes,adatabaseisoverkill.Touseadatabase,youhavetoinstallandsupportaseparateserverprocess,whichoftenalsorequiresinstallingandsupportingadatabaseadministrator.YoumustlearnSQL,andwriteSQLqueriesthatconvertdatafromarelationaltoanobjectstructureandbackagain.IfyoustoreyourdataasXMLfiles,youlosetheoverheadofanextraserver.Youalsogainaneasywaytoedityourdata:

justuseatexteditor,ratherthanacomplicateddatabasetool.XMLfilesarealsoeasiertobackup,tosharewithyourfriends,ortodownloadtoyourclients.Youcanalsoeasilyuploadnewdatatoyoursite,usingFTP.

AmoreabstractadvantageofXMListhat,beingahierarchicalratherthanarelationalformat,itcanbeusedinamuchmorestraightforwardmannertodesigndatastructuresthatfityourneeds.Youdon'tneedtouseanentityrelationshipeditornornormalizeyourschema.Ifyouhaveoneelementthatcontainsanotherelement,youcanrepresentthatdirectlyintheformat,ratherthanusingajointable.

Notethatformanyapplications,afilesystemwillnotsuffice.Ifyouhaveahighvolumeofupdates,afilesystemmaygetconfusedorcorruptedbysimultaneouswrites;databasesusuallysupporttransactions,whichallowconcurrencywithoutcorruption.Further,adatabaseisanexcellenttoolifyouneedtomakecomplicatedqueries,especiallyiftheywillvaryfromtimetotime.Databasesbuildindexes,andareoptimizedforkeepingtheindexesuptodatewithaconstantlychangingdataset.Relationaldatabasesalsohavemanyotheradvantages,includingarichquerylanguage,matureauthoringandschemadesigntools,provenscalability,fine-grainedaccesscontrol,andsoon.

(Note:

Youcanusesimplefilelockingtoprovideapoorman'stransactionserver.AndyoucanalsoimplementanXMLindex-and-searchtoolinJava,butthat'satopicforanotherarticle.)

Inthiscase,asinmostlow-to-mediumvolume,publishing-basedWebsites,youcanassumethefollowing:

mostofthedataaccessisreads,notwrites;thedata,thoughpotentiallylarge,isrelativelyunchanging;youwon'tneedtodocomplicatedsearches,butifyoudo,you'lluseaseparatesearchengine.TheadvantagesofusingamatureRDBMSfade,whiletheadvantageofusinganobject-orienteddatamodelcometothefore.

Finally,it'sentirelypossibletoprovideawrapperforyourdatabasethatmakesSQLqueriesandtranslatesthemintoXMLstreams,soyoucouldhaveitbothways.XMLbecomesamorerobust,programmer-friendlyfrontendtoamaturedatabaseforstoringandsearching.(Oracle'sXSQLservletisoneexampleofthistechnique.)

Theapplication:

Anonlinephotoalbum

Everybodylovesphotos!

Peopleloveshowingpicturesofthemselves,theirfriends,theirpets,andtheirvacations.TheWebistheultimatemediumforself-indulgentshutterbugs--theycanannoytheirrelativesfromthousandsofmilesaway.Whileafull-fledgedphotoalbumsitewouldrequireacomplicatedobjectmodel,I'llfocusondefiningasinglePictureobject.(ThesourcecodeforthisapplicationisavailableinResources.)Theobjectrepresentingapictureneedsfieldsrepresentingitstitle,thedateitwastaken,anoptionalcaption,and,obviously,apointertotheimagesource.

Animage,inturn,needsafewfieldsofitsown:

thelocationofthesourcefile(aGIForJPEG)andtheheightandwidthinpixels(toassistyouinbuildingtags).Herethereisoneneatadvantagetousingthefilesystemasyourdatabase:

youcanstoretheimagefilesinthesamedirectoryasthedatafiles.

Finally,let'sextendthepicturerecordwithanelementdefiningasetofthumbnailimagesforuseinthetableofcontentsorelsewhere.HereIusethesameconceptofimageIdefinedearlier.

TheXMLrepresentationofapicturecouldlooksomethinglikethis:

AlexOnTheBeach

1999-08-08

Tryinginvaintogetatan

alex-beach.jpg

340

200

alex-beach-sm.jpg

72

72

alex-beach-med.jpg

150

99

NotethatbyusingXML,youputalltheinformationaboutasinglepictureintoasinglefile,ratherthanscatteringitamongthreeorfourseparatetables.Let'scallthisa.pixfile--soyourfilesystemmightlooklikethis:

summer99/alex-beach.pix

summer99/alex-beach.jpg

summer99/alex-beach-sm.jpg

summer99/alex-beach-med.jpg

summer99/alex-snorkeling.pix

etc.

 

Techniques

There'smorethanonewaytoskinacat,andthere'smorethanonewaytobringXMLdataontoyourJSPpage.Hereisalistofsomeofthoseways.(Thislistisnotexhaustive;manyotherproductsandframeworkswouldserveequallywell.)

DOM:

YoucanuseclassesimplementingtheDOMinterfacetoparseandinspecttheXMLfile

XMLEntryList:

YoucanusemycodetoloadtheXMLintoajava.util.Listofname-valuepairs

XPath:

YoucanuseanXPathprocessor(likeResin)tolocateelementsintheXMLfilebypathname

XSL:

YoucanuseanXSLprocessortotransformtheXMLintoHTML

Cocoon:

YoucanusetheopensourceCocoonframework

Rollyourownbean:

YoucanwriteawrapperclassthatusesoneoftheothertechniquestoloadthedataintoacustomJavaBean

NotethatthesetechniquescouldbeappliedequallywelltoanXMLstreamyoureceivefromanothersource,suchasaclientoranapplicationserver.

JavaServerPages

TheJSPspechashadmanyincarnations,anddifferentJSPproductsimplementdifferent,incompatibleversionsofthespec.IwilluseTomcat,forthefollowingreasons:

Itsupportsthemostup-to-dateversionsoftheJSPandservletspecs

It'sendorsedbySunandApache

YoucanrunitstandalonewithoutconfiguringaseparateWebserver

It'sopensource

(FormoreinformationonTomcat,seeResources.)

YouarewelcometouseanyJSPengineyoulike,butconfiguringitisuptoyou!

BesurethattheenginesupportsatleasttheJSP1.0spec;thereweremanychangesbetween0.91and1.0.TheJSWDK(JavaServerWebDevelopmentKit)willworkjustfine.

TheJSPstructure

WhenbuildingaJSP-drivenWebsite(alsoknownasaWebapp),Iprefertoputcommonfunctions,imports,constants,andvariabledeclarationsinaseparatefilecalledinit.jsp,locatedinthesourcecodeforthisarticle.

IthenloadthatfileintoeachJSPfileusing<%@includefile="init.jsp"%>.The<%@include%>directiveactsliketheClanguage's#include,pullinginthetextoftheincludedfile(here,init.jsp)andcompilingitasifitwerepartoftheincludingfile(here,picture.jsp).Bycontrast,the

include>tagcompilesthefileasaseparateJSPfileandembedsacalltoitinthecompiledJSP.

Findingthefile

WhentheJSPstarts,thefirstthingitneedstodoafterinitializationisfindtheXMLfileyouwant.Howdoesitknowwhichofthemanyfilesyouneed?

TheanswerisfromaCGIparameter.TheuserwillinvoketheJSPwiththeURLpicture.jsp?

file=summer99/alex-beach.pix(orbypassingafileparameterthroughanHTMLform).

However,whentheJSPreceivestheparameter,you'restillonlyhalfwaythere.Youstillneedtoknowwhereonthefilesystemtherootdirectorylies.Forexample,onaUnixsystem,theactualfilemaybeinthedirectory/home/alex/public_html/pictures/summer99/alex-beach.pix.JSPsdonothaveaconceptofacurrentdirectorywhileexecuting,soyouneedtoprovideanabsolutepathnametothejava.iopackage.

TheServletAPIprovidesamethodtoturnaURLpath,relativetothecurrentJSPorServlet,intoanabsolutefilesystempath.ThemethodServletContext.getRealPath(String)doesthetrick.EveryJSPhasaServletContextobjectcalledapplication,sothecodewouldbe:

Stringpicturefile=

application.getRealPath("/"+request.getParameter("file"));

or

Stringpicturefile=

getServletContext().getRealPath("/"+request.getParameter("file"));

whichalsoworksinsideaservlet.(Youmustappenda/becausethemethodexpectstobepassedtheresultsofrequest.getPathInfo().)

Oneimportantnote:

wheneveryouaccesslocalresources,beverycarefultovalidatetheincomingdata.Ahacker,oracarelessuser,cansendbogusdatatohackyoursite.Forinstance,considerwhatwouldhappenifthevaluefile=../../../../etc/passwdwereentered.Theusercouldinthiswayreadyourserver'spasswordfile.

TheDocumentObjectModel

DOMstandsfortheDocumentObjectModel.ItisastandardAPIforbrowsingXMLdocuments,developedbytheWorld

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2