©2012MartyHallCustomizedJavaEETraining::©2012MartyHallCustomizedJavaEETraining:://courses.coreservlets.com/.JSF2.0,PrimeFaces,Servlets,JSP,Ajax(withjQuery),GWT,Androiddevelopment,Java6and7programming,SOAP-basedandRESTfulWebServices,Spring,Hibernate/JPA,XML,Hadoop,andcustomizedcombinationsoftopics.TaughtbytheauthorofCoreServletsandJSP,MoreServletsandJSP,andthistutorial.Availableatpublicvenues,orcustomizedversionscanbeheldon-siteatyourorganization.Contacthall@coreservlets.comfordetails.Agenda•Staticvs.dynamictext•DynamiccodeandgoodJSPdesign•JSPexpressions•Servletsvs.JSPpagesforsimilartasks•JSPscriptlets•JSPdeclarations•Predefinedvariables•Comparisonofexpressions,scriptlets,anddeclarations•XMLsyntaxforJSPpages4©2012MartyHallCustomizedJavaEETraining:•Scriptingelementscallingservletcodedirectly•Scriptingelementscallingservletcodeindirectly(bymeansofutilityclasses)•Beans•Servlet/JSPcombo(MVC)•MVCwithJSPexpressionlanguage•Customtags•MVCwithbeans,customtags,andaframeworklikeJSF2.06SimpleApplicationComplexApplicationDesignStrategy:LimitJavaCodeinJSPPages•Youhavetwooptions–Put25linesofJavacodedirectlyintheJSPpage–Putthose25linesinaseparateJavaclassandput1lineintheJSPpagethatinvokesit•Whyisthesecondoptionmuchbetter?–Development.YouwritetheseparateclassinaJavaenvironment(editororIDE),notanHTMLenvironment–Debugging.Ifyouhavesyntaxerrors,youseethemimmediatelyatcompiletime.Simpleprintstatementscanbeseen.–Testing.Youcanwriteatestroutinewithaloopthatdoes10,000testsandreapplyitaftereachchange.–Reuse.Youcanusethesameclassfrommultiplepages.7BasicSyntax•HTMLText–H1Blah/H1–Passedthroughtoclient.Reallyturnedintoservletcodethatlookslike•out.print(H1Blah/H1);•HTMLComments–!--Comment--–SameasotherHTML:passedthroughtoclient•JSPComments–%--Comment--%–Notsenttoclient•Escaping%–Toget%inoutput,use\%8TypesofScriptingElements•Expressions–Format:%=expression%–Evaluatedandinsertedintotheservlet’soutput.I.e.,resultsinsomethinglikeout.print(expression)•Scriptlets–Format:%code%–Insertedverbatimintotheservlet’s_jspServicemethod(calledbyservice)•Declarations–Format:%!code%–Insertedverbatimintothebodyoftheservletclass,outsideofanyexistingmethods•XMLsyntax–SeeslidesatendofthelectureforanXML-compatiblewayofrepresentingJSPpagesandscriptingelements9©2012MartyHallCustomizedJavaEETraining::%=value%10JSPExpressions•Format–%=JavaExpression%•Result–Expressionevaluated,convertedtoString,andplacedintoHTMLpageattheplaceitoccurredinJSPpage–Thatis,expressionplacedin_jspServiceinsideout.print•Examples–Currenttime:%=newjava.util.Date()%–Yourhostname:%=request.getRemoteHost()%•XML-compatiblesyntax–jsp:expressionJavaExpression/jsp:expression–Youcannotmixversionswithinasinglepage.YoumustuseXMLforentirepageifyouusejsp:expression.•Seeslidesatendofthislecture11JSP/ServletCorrespondence•OriginalJSPH1ARandomNumber/H1%=Math.random()%•Representativeresultingservletcodepublicvoid_jspService(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType(text/html);HttpSessionsession=request.getSession();JspWriterout=response.getWriter();out.println(H1ARandomNumber/H1);out.println(Math.random());...}12JSPExpressions:Example…BODYH2JSPExpressions/H2ULLICurrenttime:%=newjava.util.Date()%LIServer:%=application.getServerInfo()%LISessionID:%=session.getId()%LITheCODEtestParam/CODEformparameter:%=request.getParameter(testParam)%/UL/BODY/HTML13PredefinedVariables•request–TheHttpServletRequest(1stargumenttoservice/doGet)•response–TheHttpServletResponse(2ndargtoservice/doGet)•out–TheWriter(abufferedversionoftypeJspWriter)usedtosendoutputtotheclient•session–TheHttpSessionassociatedwiththerequest(unlessdisabledwiththesessionattributeofthepagedirective)•application–TheServletContext(forsharingdata)asobtainedviagetServletContext().14ComparingServletstoJSP:ReadingThreeParams(Servlet)@WebServle