// generated by Dynamator Wed Mar 31 19:16:32 CST 2004
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Vector;
import javax.servlet.http.*;
public class Guestbook
extends HttpServlet
{
// following is identical to JSP
private static
class Entry
{
private String name_;
private String comment_;
public
Entry(
String name,
String comment
)
{
name_ = name;
comment_ = comment;
}
public String name() { return name_; }
public String comment() { return comment_; }
}
private static final Vector entries = new Vector();
static
{
entries.addElement(new Entry("Socrates", "To do is to be"));
entries.addElement(new Entry("Plato", "To be is to do"));
entries.addElement(new Entry("Sinatra", "Do be do be do"));
}
private static final
String
safeTrim(
String s
)
{
return s == null ? s : s.trim();
}
// above is identical to JSP
public
void
doGet(
HttpServletRequest request,
HttpServletResponse response
)
throws IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
output(request, response, out);
out.flush();
out.close();
}
public
void
doPost(
HttpServletRequest request,
HttpServletResponse response
)
throws IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
output(request, response, out);
out.flush();
out.close();
}
public static
void
output(
HttpServletRequest request,
HttpServletResponse response,
PrintWriter out
)
{
// following is identical to JSP
String name = "";
String comment = "";
Dictionary fieldErrors = new Hashtable();
String pageMessage = null;
if ( "POST".equals(request.getMethod()) )
{
name = safeTrim(request.getParameter("name"));
comment = safeTrim(request.getParameter("comment"));
if ( name == null || name.length() == 0 )
{
fieldErrors.put("name", "Please enter your name");
}
if ( comment == null || comment.length() == 0 )
{
fieldErrors.put("comment", "Please enter a comment");
}
if ( fieldErrors.isEmpty() )
{
entries.addElement(new Entry(name, comment));
pageMessage = "Thank you for signing my guestbook!";
name = "";
comment = "";
}
else
{
pageMessage = "Please correct the items below";
}
}
// above is identical to JSP
out.write("\n");
out.write("\n");
out.write("
\n");
out.write(" \n");
out.write(" \n");
out.write(" My Guestbook\n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" My Guestbook
\n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" ");
out.write(String.valueOf(pageMessage != null ? pageMessage : " "));
out.write(" | \n");
out.write("
\n");
out.write("
\n");
out.write(" \n");
out.write(" \n");
out.write("
\n");
out.write(" \n");
out.write(" Guest Comments
\n");
out.write(" \n");
out.write(" \n");
out.write(" Name | \n");
out.write(" Comment | \n");
out.write("
\n");
{
final java.util.Vector $entries = entries;
final int lim$entries = $entries.size();
Entry entry;
for ( int i$entries = 0; i$entries < lim$entries; ++i$entries )
{
entry = (Entry) $entries.elementAt(i$entries);
out.write(" \n");
out.write(" ");
out.write(String.valueOf(entry.name()));
out.write(" | \n");
out.write(" ");
out.write(String.valueOf(entry.comment()));
out.write(" | \n");
out.write("
\n");
}
}
out.write(" \n");
out.write("
\n");
out.write(" \n");
out.write("\n");
}
}