Danh mục tài liệu

Using JavaBeans in JavaServer Pages - Chương 3

Số trang: 31      Loại file: ppt      Dung lượng: 83.50 KB      Lượt xem: 17      Lượt tải: 0    
Xem trước 4 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Session Objectives Describe the various features of a JavaBean Differentiate between a JavaBean and a Java class Describe the basic structure of a JavaBean Describe the various ways of creating beans Describe the advantages of using beans Explain how to access beans through JSP scriptlets Describe the use of various JSP bean tags Define the scope of beans
Nội dung trích xuất từ tài liệu:
Using JavaBeans in JavaServer Pages - Chương 3 UsingJavaBeansin JavaServerPages Chương3 SessionObjectives DescribethevariousfeaturesofaJavaBean  DifferentiatebetweenaJavaBeanandaJavaclass DescribethebasicstructureofaJavaBean Describethevariouswaysofcreatingbeans Describetheadvantagesofusingbeans ExplainhowtoaccessbeansthroughJSPscriptlets DescribetheuseofvariousJSPbeantags Definethescopeofbeans /2of36 WhatisaJavaBean? JavaBeansbringscomponenttechnology totheJavaplatform WiththehelpoftheJavaBeansAPI,youcancreate reusableandplatformindependentcomponents Thesecomponentscanbecombinedintoapplets, applications,orcompositecomponentsJavaBeanoraBean,isasimpleJavaclassthatfollows asetofnaminganddesignconventions, outlinedbytheJavaBeansspecifications /3of36 Differencebetweena JavaBeanandaJavaclassAbeanisaJavaclasswiththefollowingadditionalcharacteristics: Theclassmustbeinstantiable Itmusthaveadefaultconstructor Itmustbeserializable ItmustfollowtheJavaBeansdesignpatterns /4of36 JavaBeansDesignPatterns Namingconventionsforbeanmethodsand datamembers Designpatternsareimportantfor introspection  Introspection:mechanismusingcomponents makesitsinternalstructureknowntotheoutside world? Methodsareexposedthroughreflection /5of36 AnExampleofaJavaBean Propertypublic class Tower { private float height; public float getHeight() { getPropertyName() return height; } public void setHeight(float h) { height = h; } setPropertyName() public boolean isGreaterHeight(int initialHeight, int finalHeight) { if((finalHeight - initialHeight) > 0) { return true; } else { return false;} } public Tower() { height = (float)10.5; } } /6of36 UsingJSPBeanTags JSPprovidesthreebasicbeantags:  Tofindandusethebean–jsp:useBean  Tosetoneormoreproperties–jsp:setProperty  Togetaproperty–jsp:getProperty Thesearealsoknownasactionsandare specifictagsthataffectthefollowing:  theruntimebehavioroftheJSP  theresponsesthataresentbacktotheclient /7of36UsingJSPBeanTags–(1) getProperty_Name() setProperty_Name(value) TheJSPneednotknowthe innerstructureofthebean Bean TheBlackboxapproach /8of36 jsp:useBean  ThisisusedtoassociateaJavaBeaninJSP.  Itensuresthattheobjectcanbereferenced fromJSP,usinganIDandscope Thebeanclass mustbeavailable  Thesyntaxisasfollows: totheJSPengine jsp:useBean–(1) beanDetailsisoneof:  class = className  class = className type = typeName  beanName = beanName type = typeName  type = typeName /10of36 ScopeofBeans page  Thebeanwilldisappearassoonasthecurrentpage finishesgenerating  Referencestothisobjectwillonlybereleasedafterthe responseissentbacktotheclient request  Thebeaninstancewillbeavailableaslongasthe requestobjectisavailable  Referencestothisobjectwillbereleasedonlyafterthe requestisprocessedcompletely /11of36 ScopeofBeans–(1) session  Theinstancewillbeavailableacrossaparticularsession betweenthebrowserofaparticularmachineandthe Webserver  Referencestothisobjectwillbereleasedonlyafterthe associatedsessionsend application  Theinstancewillbeavailabletoallusersandpagesof theapplication  Itisreleasedonlywhentheruntimeenvironment reclaimstheServletContext /12of36 AnExample Thescopeisset. tothesession.This is used to instantiate the bean Hello World! /13of36 SessionScope Beans with session scope are accessible only within pages processing requests that are in the same session as the one in which the bean was created Beans cannot be defined in a page whose page directivehasanattributesession=false References to the session scope are stored in the sessionobject Thebeanisdistinctforeveryclientandisvaluable aslongastheclient’ssessionisvalid /14of36 SessionScope–(1) AnExampleThefirstJSP . . . Instantiatethe Counterbean A Session bean: The First Example The cur ...