Java Server Pages - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Java Server Pages

Description:

... constructor and bean info classes make using JavaBeans easier for IDEs ... They allow IDEs (like VisualAge, J , VisualCafe) to provide reusable components ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 41
Provided by: randyf3
Category:
Tags: ides | java | pages | server

less

Transcript and Presenter's Notes

Title: Java Server Pages


1
Java Server Pages
  • Continued

2
JavaBeans
  • Reusable Components for Java

3
What is a JavaBean?
  • A Java class meeting specific requirements
  • Must have a zero-argument constructor
  • E.g. public MyBean()
  • Must have no public fields
  • All fields should be private (encapsulation)
  • All data should be accessed via access methods
  • Allow you to define properties
  • These are virtual exposed fields
  • Similar in nature to ActiveX controls

4
JavaBean Properties
  • Read-only properties
  • int getWidth() returns the width property
  • Read/write properties
  • void setHeight(int iHeight)
  • int getHeight()
  • Boolean properties
  • boolean isSold()
  • void setSold(boolean bSold)

5
More About JavaBeans
  • JavaBeans can receive events, or cause events in
    other beans (or classes)
  • JavaBeans usually implement the Serializable
    interface to allow them to be stored
  • JavaBeans provide other methods (non-access
    methods) for their interfaces
  • Properties and methods of JavaBeans can be
    determined at runtime, using a technique known as
    introspection
  • Define these properties and methods in a bean
    info class
  • I.e. A class that implements BeanInfo

6
Whats the Point?
  • JavaBeans provide a framework for reusable
    components
  • The zero-argument constructor and bean info
    classes make using JavaBeans easier for IDEs
  • The properties can be easily connected
    graphically to components such as text fields,
    radio buttons, etc.
  • They allow IDEs (like VisualAge, J, VisualCafe)
    to provide reusable components similar to those
    used by VisualBasic

7
JavaBean Example
  • public class PythagorasBean
  • private double dWidth 0.0
  • private double dHeight 0.0
  • public void setWidth(double dWidth)
  • this.dWidth dWidth
  • public double getWidth() return dWidth
  • public void setHeight(double dHeight)
  • this.dHeight dHeight
  • public double getHeight() return dHeight
  • public double getHypoteneuse()
  • return Math.sqrt((dWidthdWidth)(dHeightdHeigh
    t))

8
When to use JavaBeans
  • Client Applications or Applets
  • GUI and non-GUI beans can be combined in a rapid
    application development app
  • Server tier
  • Used to represent server tier data model
  • Can be conveniently accessed from JSPs

9
JavaBeans and JSPs
  • Using JavaBeans from Java Server Pages

10
Creating JavaBean Instances
  • ltjspuseBean idbean1 classcs334.MyBean /gt
  • Creates a bean instance called bean1 of type
    cs334.MyBean
  • Similar to the following JSP code
  • lt cs334.MyBean bean1 new cs334.MyBean() gt

11
Instances with Different Types
  • ltjspuseBean idbean1 classcs334.MyBean
    typeObject /gt
  • Creates a bean instance called bean1 of type
    cs334.MyBean and stores it in a variable of
    type Object
  • Similar to the following JSP code
  • lt Object bean1 new cs334.MyBean() gt

12
JavaBean Scope
  • Page
  • The bean will be bound to a local variable and
    placed in the PageContext object
  • Request
  • The bean will be bound to a local variable and
    placed in the ServletRequest object
  • These beans will not last after the request is
    completed
  • The difference between these 2 scopes is very
    small
  • Beans such as this do not allow you to share data
    between servlets and JSPs

13
JavaBean Scope
  • Application
  • The bean will be bound to a local variable and
    placed in the ServletContext object
  • ServletContext is shared by all servlets and JSPs
  • Session
  • The bean will be bound to a local variable and
    also stored in the HttpSession object
  • These beans will last between requests
  • Again, the differences here are mostly cosmetic

14
JavaBean Scope
  • Page
  • ltjspuseBean idb1 classMyBean scopepage
    /gt
  • Request
  • ltjspuseBean idb1 classMyBean
    scoperequest /gt
  • Application
  • ltjspuseBean idb1 classMyBean
    scopeapplication /gt
  • Session
  • ltjspuseBean idb1 classMyBean
    scopesession /gt
  • Sound familiar? (they are scoped variables)

15
Reading Bean Properties
  • ltjspgetProperty namebean1 propertyprop /gt
  • Inserts the value of bean1s prop property into
    the web page
  • Basically the same operation as
  • lt bean1.getProp() gt

16
Setting Bean Properties
  • ltjspsetProperty namebean1 propertyprop
    valueabc /gt
  • Sets the value of bean1s prop property to
    abc
  • Basically the same operation as
  • lt bean1.setProp(abc) gt

17
Setting Bean Properties Using JSP Parameters
  • ltjspsetProperty namebean1 propertyprop
    paramabc /gt
  • Sets the value of bean1s prop property to the
    value of the JSP parameter named abc
  • If the parameter does not exist, the value of the
    bean property does not change
  • Similar to
  • lt bean1.setProp(request.getParameter(abc)) gt
  • Except that it checks if the parameter is null
    before setting the value

18
Setting Bean Properties Using JSP Parameters
  • ltjspsetProperty namebean1 property /gt
  • Sets the value of all bean1s properties to JSP
    parameters with the same name
  • If the parameters do not exist, the value of the
    bean properties do not change

19
An Example JSP
  • lthtmlgt
  • ltbodygt
  • ltjspuseBean idpyth classPythagorasBean
    /gt
  • lth2gtThe hypoteneuse of the triangle islt/h2gt
  • ltjspsetProperty namepyth propertywidth
    value4 /gt
  • ltjspsetProperty namepyth propertyheight
    value5 /gt
  • ltjspgetProperty namepyth propertyhypoteneu
    se /gt
  • lt/bodygt
  • lt/htmlgt

20
Demonstration
  • simple_bean.jsp

21
JavaBean Number Converter
  • For additional practice, I have created a
    JavaBean which converts integer numbers into
    their English equivalent (and vice versa)
  • This bean is used as an example which allows us
    to remove most code from the JSP, and put it
    elsewhere

22
Taglibs
  • Customized JSP Tags

23
Taglibs
  • Offer no technological advantage over servlets,
    but do provide convenience
  • Custom JSP tags associated with tag handler
    classes
  • The tag handler classes do the work that the
    custom tags represent
  • Present a convenient method for code
    modularization in JSPs

24
When would you use taglibs?
  • The same reasons they created HTML and JSP tags
  • Whenever you need to perform the same operation
    in several places
  • Examples
  • A tag that generates a view of the users
    shopping cart, including total price
  • A tag that filters text (the body of the tag) for
    swearing, etc.

25
How do you do it?
  • Define a class that generates HTML
  • This class can access
  • JSP/Servlet-related information
  • E.g. Parameters, Session
  • The text in the body of the tag
  • Create a tag library descriptor file
  • XML file containing a description of the JSP tags
    you create
  • Use the tag library from within a JSP file

26
A Simple TagHandler Class
  • public class RandomNumberTag extends TagSupport
  • public int doStartTag()
  • try
  • JspWriter out pageContext.getOut()
  • out.print(Math.random())
  • catch (IOException e)
  • e.printStackTrace()
  • return SKIP_BODY // we dont use the tag body

27
Tag Handler Descriptor File
  • lttaglibgt
  • lttlib-versiongt1.0lt/tlib-versiongt
  • ltjsp-versiongt1.1lt/jsp-versiongt
  • ltshort-namegtcs334lt/short-namegt
  • ltdescriptiongtA sample tag librarylt/descriptiongt
  • lttaggt
  • ltnamegtrandomlt/namegt
  • lttag-classgtRandomNumberTaglt/tag-classgt
  • ltbody-contentgtEMPTYlt/body-contentgt
  • ltdescriptiongtPrints a random
    numberlt/descriptiongt
  • lt/taggt
  • lt/taglibgt

28
The JSP File
  • lt_at_ taglib urics334_taglib.tld prefixcs334
    gt
  • lthtmlgt
  • lth2gtA random numberlt/h2gt
  • ltcs334random /gt
  • lth2gtAnother random numberlt/h2gt
  • ltcs334random /gt
  • lt/htmlgt

29
Demonstration
  • simple_taglib.jsp

30
Tag Attributes
  • If you want to define an attribute called max,
    just define a method like
  • public void setMax(String value)
  • Define this method in the tag handler class

31
Tag Attributes
  • The JSP file will look something like
  • ltcs334random max10gt
  • The taglib definition file will look like
  • lttaggt
  • ltnamegtrandomlt/namegt
  • ltattributegt
  • ltnamegtmaxlt/namegt
  • ltrequiredgtfalselt/requiredgt
  • lt/attributegt

32
Demonstration
  • attrib_taglib.jsp

33
Tag Body Text
  • The HTML text inside the tag can be retrieved
    using the method
  • String strBody getBodyContent().getString()
  • Obviously, this is done in the tag handler class
  • You can now manipulate this body text, and print
    it using the normal out.print() and out.println()
    calls

34
Example
  • A custom tag that converts everything into
    uppercase
  • You can see how this knowledge could be used to
    develop any filtering tag

35
Uppercase Tag Handler
  • public class UppercaseTag extends BodyTagSupport
  • public int doAfterBody()
  • try
  • BodyContent body getBodyContent()
  • JspWriter out body.getEnclosingWriter()
  • String strBody body.getString()
  • out.print(strBody.toUpperCase())
  • catch (IOException e)
  • e.printStackTrace()
  • return SKIP_BODY // dont print the tag body
    again

36
Uppercase THD
  • lttaglibgt
  • lttlib-versiongt1.0lt/tlib-versiongt
  • ltjsp-versiongt1.1lt/jsp-versiongt
  • ltshort-namegtcs334lt/short-namegt
  • ltdescriptiongtA sample tag librarylt/descriptiongt
  • lttaggt
  • ltnamegtuppercaselt/namegt
  • lttag-classgtUppercaseTaglt/tag-classgt
  • ltbody-contentgtJSPlt/body-contentgt
  • ltdescriptiongtConverts to upper
    caselt/descriptiongt
  • lt/taggt
  • lt/taglibgt

37
The JSP File
  • lt_at_ taglib urics334_taglib.tld prefixcs334
    gt
  • lthtmlgt
  • lt/bodygt
  • lth2gtHere is the testlt/h2gt
  • ltcs334uppercasegt
  • This text is both uppercAse and lowerCASE.
  • lt/cs334uppercasegt
  • lt/bodygt
  • lt/htmlgt

38
Demonstration
  • uppercase_taglib.jsp

39
JavaBean Example
  • A Bean for Converting Numbers to/from English
    Written Notation

40
Taglib Examples
  • An Internationalization Tag Library
Write a Comment
User Comments (0)
About PowerShow.com