EBIZ 5535 Lecture 4 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

EBIZ 5535 Lecture 4

Description:

The import attribute is the only one that is allowed to appear multiple times. ... Use the param attribute of setProperty to indicate that ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 26
Provided by: jeffreyv
Category:

less

Transcript and Presenter's Notes

Title: EBIZ 5535 Lecture 4


1
EBIZ 5535Lecture 4
  • JSP Directives and Actions

2
Outline
  • JSP Directives
  • Page directive
  • Include directive
  • JSP Actions
  • jspinclude
  • jspuseBean
  • jspsetProperty
  • jspgetProperty
  • jspforward

3
JSP Directives
  • A JSP directive affects the overall structure of
    the servlet class
  • lt_at_ directive attribute"value" gt
  • Or
  • lt_at_ directive attribute1"value1"
  • attribute2"value2"
  • ...
  • attributeN"valueN" gt

4
Types of Directives
  • There are two main types of directive
  • page, which lets you do things like import
    classes, customize the servlet superclass
  • include, which lets you insert a file into the
    servlet class at the time the JSP file is
    translated into a servlet.

5
The JSP page Directive
  • Lets you define one or more of the following
    case-sensitive attributes
  • import"package.class" or import"package.class1,.
    ..,package.classN".
  • Examplelt_at_ page import"java.util." gtThe
    import attribute is the only one that is allowed
    to appear multiple times.
  • contentType"MIME-Type" or contentType"MIME-Type
    charsetCharacter-Set"Examplelt_at_ page
    contentType"text/plain" gt

6
More Page Attributes
  • errorPage"url". This specifies a JSP page that
    should process any Throwables thrown but not
    caught in the current page.
  • isErrorPage"truefalse". This indicates whether
    or not the current page can act as the error page
    for another JSP page. The default is false.
  • isThreadSafe"truefalse"
  • session"truefalse"
  • buffer"sizekbnone" This specifies the buffer
    size for out. The default is server-specific, but
    must be at least 8kb

7
More Page Attributes
  • autoflush"truefalse". A value of true, the
    default, indicates that the buffer should be
    flushed when it is full.
  • extends"package.class". This indicates the
    superclass of servlet that will be generated. Use
    this with extreme caution, since the server may
    be using a custom superclass already.
  • info"message". This defines a string that can be
    retrieved via the getServletInfo method

8
The JSP include Directive
  • Lets you include files at the time the JSP page
    is translated into a servlet
  • lt_at_ include file"relative url" gt
  • Interpreted as a JSP.
  • You can tell the system to interpret the URL
    relative to the home directory of the Web server
    by starting the URL with a forward slash.

9
Example of Using Include
  • lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • ltHTMLgt
  • ltHEADgt
  • ltTITLEgtJavaServer Pages (JSP) 1.0lt/TITLEgt
  • lt/HEADgt
  • ltBODYgt
  • lt_at_ include file"/navbar.html" gt
  • lt!-- Part specific to this page ... --gt
  • lt/BODYgt
  • lt/HTMLgt

10
Example
  • JspTest.jsp

11
JSP Actions
  • JSP actions use constructs in XML syntax to
    control the behavior of the servlet engine.
  • jspinclude - Include a file at the time the page
    is requested
  • jspuseBean - Find or instantiate a JavaBean
  • jspsetProperty - Set the property of a JavaBean
  • jspgetProperty - Insert the property of a
    JavaBean into the output
  • jspforward - Forward the requester to a new page
  • jspplugin - Generate browser-specific code that
    makes an OBJECT or EMBED tag for the Java plugin

12
The jspinclude Action
  • This action lets you insert files into the page
    being generated
  • ltjspinclude page"relative URL" flush"true" /gt
  • Unlike the include directive, this action inserts
    the file at the time the page is requested
  • Precludes the included page from containing
    general JSP code (it cannot set HTTP headers, for
    example), but it gains significantly in
    flexibility

13
Example
  • lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • ltHTMLgt ltHEADgt
  • ltTITLEgtWhat's Newlt/TITLEgt
  • lt/HEADgt
  • ltBODY BGCOLOR"FDF5E6" TEXT"000000
  • LINK"0000EE" VLINK"551A8B" ALINK"FF0000"gt
  • ltCENTERgt
  • ltTABLE BORDER5 BGCOLOR"EF8429"gt ltTRgtltTH
    CLASS"TITLE"gt What's New at JspNews.comlt/TABLEgt
  • lt/CENTERgt
  • ltPgt Here is a summary of our four most recent
    news stories ltOLgt ltLIgtltjspinclude
    page"news/Item1.html" flush"true"/gt
    ltLIgtltjspinclude page"news/Item2.html"
    flush"true"/gt ltLIgtltjspinclude
    page"news/Item3.html" flush"true"/gt
    ltLIgtltjspinclude page"news/Item4.html"
    flush"true"/gt lt/OLgt
  • lt/BODYgt lt/HTMLgt

14
The jspuseBean Action
  • This action lets you load in a instance to be
    used in the JSP page
  • Useful because it lets you exploit the
    reusability of Java classes without sacrificing
    the convenience that JSP adds
  • ltjspuseBean id"name" class"package.class" /gt
  • Usually, instantiate an object (called a bean) of
    the class specified by class and bind it to
    variable name

15
jspgetProperty/setProperty
  • Once you have a bean, you can modify its
    properties via jspsetProperty, or by using a
    scriptlet
  • With beans, "this bean has a property of typeX
    called foo", really means "this class has a
    method called getFoo that returns something of
    type X, and another method called setFoo that
    takes an X as an argument

16
Example
  • lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • ltHTMLgt ltHEADgt
  • ltTITLEgtReusing JavaBeans in JSPlt/TITLEgt
  • lt/HEADgt
  • ltBODYgt ltCENTERgt
  • ltTABLE BORDER5gt ltTRgt
  • ltTH CLASS"TITLE"gt Reusing JavaBeans in
    JSPlt/TABLEgt
  • lt/CENTERgt
  • ltPgt ltjspuseBean id"test" class"SimpleBean" /gt
  • ltjspsetProperty name"test" property"message"
    value"Hello WWW" /gt
  • ltH1gtMessage ltIgt ltjspgetProperty name"test"
    property"message" /gt lt/Igtlt/H1gt lt/BODYgt lt/HTMLgt

17
Example contd
  • public class SimpleBean
  • private String message "No message
    specified"
  • public String getMessage() return(message)
  • public void setMessage(String message)
    this.message message
  • Note Install beans in the servlet directory

18
Equivalent to Scriptlets
  • ltjspuseBean id"test" class"SimpleBean" /gt
  • Equivalent scriptlet
  • lt SimpleBean test new SimpleBean() gt
  • ltjspsetProperty name"test" property"message"
    value"Hello WWW" /gt
  • Equivalent scriptlet
  • lt test.setMessage(Hello WWW) gt

19
Equivalent Scriptlets contd
  • ltjspgetProperty name"test" property"message"
    /gt
  • Equivalent scriptlet
  • lt test.getMessage() gt

20
Setting Bean Properties to JSP Page Parameters
  • Typically, one wants to use a bean to process the
    parameters sent to a JSP page.
  • jspsetProperty is set up to pass parameters to a
    bean very easily
  • What we want is something like this on our JSP
    page
  • ltjspsetProperty nametest propertymessage
  • value lttest.getParam(message) gt

21
  • http//www.myserver.edumyport/mypage.jsp?message
    hithere
  • When this page is accessed, the setMessage method
    of the bean is called with argument hi there

22
Conversion of Non-String Parameters
  • All parameters have string values, but these
    values are converted when bean properties have
    other types
  • lt int numItems 1
  • try
  • numItems Integer.parseInt
  • (request.getParameter
  • (numItems))
  • catch (NumberFormatException e)
  • gt

23
  • ltjspsetProperty name entry
  • propertynumItems
  • value ltrequest.getProperty(numItems) gt
  • /gt
  • Equivalent to
  • ltjspsetProperty name entry
  • propertynumItems
  • paramnumItems /gt

24
So
  • Use the param attribute of setProperty to
    indicate that
  • Value should come from specified request
    parameter
  • Simple automatic type conversion should be
    performed

25
Associating All Properties with Input Parameters
  • Use for the value of the property attribute
    of jspproperty to indicate that
  • Value should come from request parameter whose
    name matches property name
  • Simple automatic type conversion should be
    performed
  • ltjspsetProperty nametest
  • property /gt
Write a Comment
User Comments (0)
About PowerShow.com