Workshop 4. JSP Directives
Objective
There are two objectives to this workshop: first is to format a number for display and the second objective is to convert a String to double so that some basic maths can be carried out.
Time Available
This workshop should take no longer than 60 minutes.
Directives Workshop
The following demonstrates the objective of this workshop.
- In the "JSP Module" folder create a new JSP named "directives.jsp" and add the following
HTML.
<html> <head> <title>Directives</title> </head> <body> <% //Declare number and string double dblSalary = 5000; String strRent = "400"; %> <p> Number formatted as string: <b><%= makeString(dblSalary) %></b> </p> <% //Increase rent by 10% double dblNewRent = makeDouble(strRent); dblNewRent = dblNewRent + (dblNewRent * 0.1); %> <p> String converted to number and back again: <b><%= makeString(dblNewRent) %></b> </p> </BODY> </HTML>
- In the above code we can see two local methods being used
makeString()andmakeDouble(). Add a declaration section to your JSP and that includes the declaration of these two methods:
<%! //Method for formatting a double as a string private String makeString(double n) { } //Method for formatting a string to a double private double makeDouble(String s) { } %> - Now add the code to
makeString()that will convert a double to a String. Java has some clever methods that offer great flexibility and the following code is one such example,
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.UK); return nf.format(n);
- Now the
NumberFormatclass is defined in the packagejava.textand theLocaleclass is defined in the packagejava.util. Add a directive that references both these classes.
- Similarly add code to
makeDouble()that will convert a double to a String:
Double d = new Double(s); return d.doubleValue();
- The
Doubleclass is defined in the packagejava.langand this package must also be referenced in a directive.
- Save and test your work so far.
- Extra. Return to your JSP file and change the locale used in the
makeString()method to another region,Locale.USfor example, and access the JSP again to see the difference.
Now these methods for formatting numbers as currencies and converting Strings to doubles are pretty useful and it is likely that we'll want to use them again in other JSPs. This is an ideal scenario for exploiting the include directive.
- Return to your "directives.jsp" file cut the lines of code for the page import directive
and the declarations and paste these into a new file.
- Save this file as "formatting.jsp".
- Return to "directives.jsp" and add a directive to include the text from "formatting.jsp".
- Save and test these changes.
By now your code should look like this:
Tech Tips
It is a good practice to work with include files in this way. First build the JSP as a single file and then cut and paste the text into the file that is being included.
An common error that can be made with include files is placing the Java code in the include
file but forgetting the <%...%> tags. Other errors include declaring a variable
both in the include file and in the parent JSP.
A good method for debugging is to copy and paste the code from the include file to the parent JSP at the exact point of the include directive and then look for problems such as above.
Quiz !
Now that you are an expert in JSP Syntax go for top marks in the following quiz.
