计算机专业的外文翻译Word格式文档下载.docx
- 文档编号:22202344
- 上传时间:2023-02-03
- 格式:DOCX
- 页数:9
- 大小:21.23KB
计算机专业的外文翻译Word格式文档下载.docx
《计算机专业的外文翻译Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《计算机专业的外文翻译Word格式文档下载.docx(9页珍藏版)》请在冰豆网上搜索。
学号:
(用外文写)
外文出处:
JavaDeveloper’sGuide
toServletsandJSP
附件:
1.外文资料翻译译文;
2.外文原文。
指导教师评语:
签名:
年月日
注:
请将该封面与附件装订成册。
附件1:
外文资料翻译译文
附件2:
外文原文
JSPDebuggingProblems
IntheTomcatimplementation,JSPpagesarehandledbyaservletthathasitsownlogfile,called“jasper.log”bydefault.However,becausetheJSPservletisrunbythenormalservletengine,errorsduringcompilationpresentlyendupinthemain“tomcat.log”file.IfyouareusinganotherservletandJSPengine,itwillprobablyhavedifferentlogfileconventions.
Therefore,themostimportantadviceIcangiveonJSPdebuggingisthatyoushouldbecomeveryfamiliarwithalltheloggingcapabilitiesofyourchosenplatform.
JSPSyntaxErrors
AJSPpagehastogothroughbothtranslationtoaJavaservletandcompilationoftheservletbeforeyouevengettomakeruntimeerrors.Youareprobablygoingtobechasingalotofsimplesyntaxproblems.AnexamplethatseemstooccuralothastodowithescapingquotationmarksinJSPexpressionstatements.TheJSPcodeinListing5.13,anexamplefromChapter3,getstranslatedintoasingleJavaout.writestatementthatwritespartofa<
form>
taginHTML.Becausethe<
tagrequiresquotedvalues,amind-bogglingnumberofescapedquotationmarksisrequired;
missinganyoneofthesecouldcauseatranslationerror,acompilationerror,oranerrorintheresultingHTMLpage.
ImportingClasses
DuetothewayJSPpagesmixHTMLandJava,itiseasytoforgettoimportrequiredclasses.TheJSPtranslatorcreatesthenecessaryimportstatementsbasedonthecodeitwrites,butyouhavetoprovideyourownforthecodeyouwrite.Whenwritingthememory.jspcodeusedlaterinthischapter,IgotthecompilationerrorshowninListing5.14.NotethattheerrormessageisformulatedinXML,adesigndecisionbySunthatwillmakecreationofmoreadvancedtoolsforJSPmucheasier.
Ifyouwadethroughtheverbiage,youcanseethatthecompilererrorisreportedasaninabilitytofindtheDateclassintheJavaclasswiththeastonishinglylongnamethatTomcatmakesupwhentranslatingJSPpages.
DesignforDebugging
Analternatenameforthissectionwouldbe“DesignforTesting,”becausethebasicideaisthatifyouplanyourJavaclassessotheycanbetestedinacontrolledenvironment,therewillbefewerbugswhentheclassesareusedinaWebserverenvironment.ThisprincipleisonereasonSunemphasizestheuseofJavaBeansinJSPandservletapplications.
ToolsforBeans
AswediscussedinChapter2,aJavaBeanissimplyaJavaclassthatmeetsthefollowingcriteria:
●Theclassmustbepublic.
●Theclassmusthaveano-argumentsconstructor.
●Theclassmustprovidesetandgetmethodstoaccessvariables.
TheJavaBeancomponentarchitecturehasbeenaroundforseveralyears,andmanyvendorsofIDEsandtoolkitshaveadaptedtheirproductstoworkwithJavaBeans.AlthoughmostofthesetoolsemphasizeJavaBeansasGUIcomponents,theyalsoworkwithbeansusedinservlets.ThecentralSunhomeWebpageforJavaBeansandrelatedtechnologyisat:
Oneofthetoolsavailableatthatsiteisthe“BeanBox,”atoolthatismainlyintendedforusewithGUIcomponentsbutcouldbeusedwithanybean.IftheJavaBeanclassisserializable,theJSPprogrammerhastheoptionofeithercreatinganewobjectfromtheclassfileorreadinginaserializedobjectwithvariablesalreadyset.
CatchThatException!
WhenworkingwithJSP,rememberthatyoucandesignateyourowncustomerrorpagebyspecifyingitinthepagedirectivetagasinthisexamplefromChapter2.
<
%@pagelanguage="
java"
errorPage="
/JSPbook/Chapt02/whoops.jsp"
%>
AnyJSPpagethatisdesignatedastheerrorpagemustincludeatagsimilartothefollowing:
isErrorPage="
true"
whichsetstheisErrorPageparameter.Thisensuresthatthepagewillhaveadefaultvariablenamedexceptionthatwillrefertotheactualerrororexception.
Whenworkingwithaservlet,itfrequentlymakessensetoenclosemostofthestatementsinthedoPostordoGetmethodwithatry-catchblockstructure.However,rememberthatyourvariabledeclarationsshouldbemadebeforethetryblockstarts;
otherwise,theywillbeoutofscopeinthecatchblock.Listing5.16showsaskeletonofadoGetmethod.NotethatittakesadvantageofthefactthatwhentheJVMistryingtofindahandlerforanexception,itwillusethefirstcatchstatementthatfitsthehierarchy.ThisallowsustoprovidespecialhandlingforIOExceptionandMyCustomExceptionexceptionswhileensuringthateveryexceptioniscaught.
CustomExceptions
WritingyourownextensionofastandardlibraryExceptionclasstofityourparticularapplicationisanexcellentwaytogetmoreinformationthanthenormalexceptionprovides.Yourcustomexceptionclasscanhaveseveraldifferentconstructorsreflectingdifferentpossibleproblems.Ifyoudon’twantthecompilertoforceyoutowriteexceptionhandlingcode,yourcustomclassshoulddescendfromRuntimeException.
UsingAssertions
AlthoughJavadoesnothaveassertioncapabilitybuiltin,itiseasytoadd.Thepurposeofanassertionstatementistoverifythatcertainconditionsaretruebeforetheprogramisallowedtoproceed.Itservesasasinglestatementshorthandforlogicthatwouldotherwisebeexpressedwithanifstatement.Forexample,ifaprogramrequiredanon-nullreferencenameduserName,youcouldcodeitlikethis:
if(userName==null){
thrownewIllegalArgumentException(“badusername”);
}
OrusingtheAssertionclass,youcouldcodeitlikethis:
Assertion.assert(userName!
=null,“badusername”);
Notethatwiththeassertion,thelogictestisexpectedtoyieldavalueoftrueundernormalconditions.Ifthetestyieldsfalse,anAssertionExceptionisthrown.Listing5.17showstheAssertionclasssourcecode,andListing5.18showsthecodefortheAssertionExceptionclass.
Youmayreasonablyask,whyuseaspecialclasswhenthesamelogiccouldbeaccomplishedwithanifstatement?
Oneansweristhatusingassertionsmakesthepurposeofthelogicveryclear.Youcantellataglancethattheprogramwouldbeinterruptedatthatpointiftheconditionisnotmet,whereasanifstatementcouldbeusedforanypurpose.
Ifyouuseassertionscorrectly,sothattheprogramdoesnotdependonaresultcausedbyanassertionstatement,thenyoucanconfidentlyremovetheassertionstatementswhenyouaresurethattheprogramisrunningcorrectly.Personally,Iprefertocommentouttheassertionstatementssothattheystayinthesourcecodeasareminderofwhatwascheckedduringprogramdevelopment.
Monitoring
Well,yougotyourapplicationrunninginyourlocalhosttestsystem.Itgivestherightresultsanddoesn’tthrowexceptions,soyoumustbefinishedwithdebugging,right?
Wrong,therearestillplentyofbugsthatcanbecomeapparentonlyafterithasbeenrunningawhile.
Forexample,youmighthaveamemoryleakdueifobjectsarebeingputintoaVectororHashtableandareneverremoved.Ifeachobjectisonlyahundredbytes,itwilltakemanyrequeststofinallykilltheserverwithamemoryerror.Yourfirstinklingofaproblemmaycomewhenyounoticethattheserverisgettingslowerandslowerduetoexcessivepagingofmemory.
AThreadleakmightoccurifeveryrequestcreatesaThreadtocarryoutsometaskbutyouhaveneglectedtoprovideawayfortheThreadtoexittherunmethodanddienormally.AfterafewthousandThreadobjectshaveaccumulated,theapplicationmaygetveryodd.
WhatyouneedforthiskindofdebuggingisawaytoremotelymonitormemoryandThreadusageinarunningserver.Thatwayyoucanlookinfromtimetotimeandwatchfortrendsthatmightindicateabug.Listing5.19showsmemory.jsp,asimpleJSPthatcandisplaythecurrentservletengineThreadsandJavaVirtualMachinememoryusage.
ThispageusesthefactthatallThreadobjectscreatedinaservletenginetypicallybelongtothesameThreadGroup.ThetoStringmethoddisplaysthenameoftheThread,itspriority,andthenameoftheThreadGroupitbelongsto.ExampleoutputfromtheTomcatWebserverusingthisJSPisshowninFigure5.5.
NotethatsomeoftheThreadobjectsshowninFigure5.5arenamed“StandardManager”andsomearenamed“Thread-n”wherenisasequencenumber.ThesequencenumbernamingstyleisthedefaultforThreadobjectscreatedwithoutaprogrammer-suppliedname.IfyourapplicationcreatesanyThreadobjects,youshouldnamethemsothattheywillstandoutinthislisting.
CustomLogging
ThemainthingtorememberwhenwritingacustomloggingfacilityisthattheremaybemanyThreadsexecutingthesameservlet,sologwritingmethodsmustbesynchronizedonthestreamobjectusedtowritethelog.Forexample,supposeyouwantedtoaddloggingtotheChatRoomclasscreatedinChapter3.ThecodeinListing5.20canbeaddedtotheinit-ChatRoomsstaticmethodtoopenthelogfileintheappendmode.
Ataminimum,acustomlogshouldprovideforloggingasimplemessageandforlogginganexceptionwithamessageasshowninListing5.21.NotethatacalltotheflushmethodisneededbecausePrintWriterstreamsdonotflushoutputbydefault.
MiscellaneousNotes
ThissectioncontainssomedebugginghintsthatIcouldnotfigureoutawaytocategorize.Therefore,Ilistthemhere:
●Notethatlogbufferingbytheservletenginemeansthatifyoulookatthelogwhiletheengineisstillrunning,youmaynotseethelatestmessages.
●Afrequentsourceofmysteriousruntimeerrorsinservletsoccurswhentheprogrammerforgetstoinclude
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机专业 外文 翻译