Workshop 3. JSP Declarations

Objective

The objective of this workshop is use a declaration to define a method which will reverse the characters of a given string.

Time Available

This workshop should take no longer than 30 minutes.

Declarations Workshop

The following demonstrates the objective of this workshop.

Demo

  1. In your working directory create a new JSP and add the following HTML.

    <html>
    <head>
    <title>Declarations</title>
    </head>
    
    <body>
    
    <p>The phrase,</p>
    
    <p><i></i></p>
    
    <p>Reads like this in reverse,</p>
    
    <p><i></i></p>
    
    </body>
    </html>
    
  2. Towards the top of your page define the following String variable, note this should be entered as a single line,
    String strQuote = "If you can keep your head when
                       all about you, Are losing theirs
                       and blaming it on you;";
    
  3. Between the first set of italics display the value of this variable.

  4. Add a declaration to your JSP that will contain a method called reverseString() which takes a String as a parameter and returns a String.

  5. Add code for this method such that it will return the given String in reverese. You'll probably want to make use of the length() and substring() methods of the string class. Check the Java API for definitions of these methods.

  6. Finally display the above String reversed in the second set of italics, something like,
    <P>Reads like this in reverse,</P>
    <P><I> <%= reverseString(strQuote) %> </I></P>
    

If you couldn't get your solution to work then click the icon to display the source code. Source code icon

Tech Tips

Other than Java coding errors there's not much that can go wrong with declarations. An obvious error is forgetting to put the ! in the JSP tags. Go back to your JSP, take this out and see what error you get.

Directives

The next topic looks at JSP Directives.

Back Next