Java Reference
In-Depth Information
LISTING 21.10
Continued
10: String[] entryFields = { “name”, “email”, “url”, “comments” };
11: String[] entry = new String[4];
12: for (int i = 0; i < entryFields.length; i++) {
13: entry[i] = Guestbook.filterString(request.getParameter(entryFields[i]));
14: }
15: Date now = new Date();
16: String entryDate = now.toString();
17: String ip = request.getRemoteAddr();
18: %>
19:
20: <p>Your entry looks like this:
21: <p>From: <%= entry[0] %>
22: <%= (!entry[1].equals(“None”) ? “<”+entry[1]+”>” : “”) %><br>
23: <% if (!entry[2].equals(“None”)) { %>
24: Home Page: <a href=”<%= entry[2] %>”><%= entry[2] %></a><br>
25: <% } %>
26: Date: <%= entryDate %><br>
27: IP: <%= ip %>
28: <blockquote>
29: <p><%= entry[3] %>
30: </blockquote>
31:
32: <%
33: try {
34: boolean append = true;
35: String filename = application.getRealPath(id + “.gbf”);
36: FileWriter fw = new FileWriter(filename, append);
37: BufferedWriter fileOut = new BufferedWriter(fw);
38: String newEntry = entry[0] + “^” + entry[1] + “^” + entry[2] + “^”
39: + entryDate + “^” + ip + “^” + entry[3];
40: fileOut.write(newEntry, 0, newEntry.length());
41: fileOut.newLine();
42: fileOut.close();
43: } catch (IOException e) {
44: out.println(“<p>This guestbook could not be updated.”);
45: log(“Guestbook Error: “ + e.toString());
46: }
47: %>
48:
49: <p><a href=”guestbook.jsp?id=<%= id %>”>View the Guestbook</a>
50: </body>
51:
52: </html>51: </html>
21
The guestbook-post.jsp page collects data from a web form, removes characters from
the data that can't be put in the guestbook, and stores the result in a text file.
 
Search WWH ::




Custom Search