Java Reference
In-Depth Information
Each guestbook has its own file with a name that begins with the ID parameter of the
book and ends with the
.gbf
file extension. If the guestbook has the ID of
cinema
, the
filename is
cinema.gbf
.
Like the other JSP pages included in this web application,
guestbook-post.jsp
can be
stored in any folder on your server where JSP pages are kept. For this project, store the
page in the same folder as
guestbook.jsp
and
cinema.gbf
.
Before you can try the
Guestbook
application, you must create a Java class that is used to
filter some unwanted text from guestbook entries before they are posted.
Three characters cannot be included in the guestbook because of the way entries are
stored in a file:
Caret characters (“^”)
n
Return characters, which have the integer value of
13
in Java
n
Linefeed characters, which have the integer value of
10
n
To remove these characters before they are saved in a guestbook, a helper class called
Guestbook
is created. This class has a static method called
filterString(
String
)
that
removes those three characters from a string.
Listing 21.11 contains the source code for this class.
LISTING 21.11
The Full Text of
Guestbook.java
1: package example;
2:
3: public class Guestbook {
4: public static String filterString(String input) {
5: input = replaceText(input, '^', ' ');
6: input = replaceText(input, (char)13, ' ');
7: input = replaceText(input, (char)10, ' ');
8: return input;
9: }
10:
11: private static String replaceText(String inString, char oldChar,
12: char newChar) {
13:
14: while (inString.indexOf(oldChar) != -1) {
15: int oldPosition = inString.indexOf(oldChar);
16: StringBuffer data = new StringBuffer(inString);
17: data.setCharAt(oldPosition, newChar);
18: inString = data.toString();
19: }
20: return inString;
21: }
22: }