Hi all,

I need to create a Java Servlet that acts as a Guestbook, taking input, writing it to a file and displaying previous entries.
I'd be grateful if someone could advise me on the following code as at the moment, it displays what the user has entered, but doesn't display previous entries and i'm not sure why.

// A servlet used to create a website Guestbook that can display a user's
// comments as well as previous comments.

import java.io.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class GuestBookServlet extends HttpServlet {

// set up
public void init( ServletConfig config ) throws ServletException
{
} // end of init method

// process Post method for the Guestbook entry.
protected void doPost( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException
{
// set content-type header before accessing the Writer
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();
DecimalFormat twoDigits = new DecimalFormat( "0.00" );

// start XHTML document and write the data of the response
out.println( "<?xml version = \"1.0\"?>" );

out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " +
"XHTML 1.0 Strict//EN\" \"http://www.w3.org" +
"/TR/xhtml1/DTD/xhtml1-strict.dtd\">" );

out.println(
"<html xmlns = \"http://www.w3.org/1999/xhtml\">" );

// head section webpage.
out.println( "<head>" );

// get Guestbook entry from user.
String Name = request.getParameter( "name" );
String Surname = request.getParameter( "surname" );
String Email = request.getParameter ( "email" );
String Comments = request.getParameter ( "comments" );

//int value =
// Integer.parseInt( request.getParameter( "choice" ) );

// submit comments and display previous comments

out.println( "<title>Thank You for your comments</title>" );
out.println( "</head>" );

if (Name.equals (""))
{out.println("You didn't fill in all fields");} //error for if name is not filled in.
else

out.println( "<body>" );
out.println( "<p><b>Thank You for your comments</b><br>");

out.println( "<p><b>You posted:</b><br>");
out.println(request.getParameter( "name" ) + "</p>" );
out.println(request.getParameter( "surname" ) + "</p>" );
out.println(request.getParameter( "email" ) + "</p>" );
out.println(request.getParameter( "comments" ) + "</p>" );
out.println( "<b>Previous comments:</b><br>" );

// store comments in text file, and return the same text file to
// display previous comments.

// end XHTML page
out.println( "</pre></body></html>" );
out.close();

} // end of doPost method

public void destroy()
{

}
//*************************************************************************
//* Code below is used to read and write data to and from the text file.
//*
//* guestbook.txt is a file that contains the Guestbook entries,
//* opt1: name
//* opt2: surname
//* opt3: email
//* opt4: comments
//************************************************************************

public void writeResults(String value, PrintWriter out, String file_name)
{


String opt1,opt2,opt3,opt4;
try
{
FileReader GuestBookData = new FileReader(new File(file_name));
BufferedReader GBreader = new BufferedReader(GuestBookData);
opt1 = GBreader.readLine(); //GuestBookData.read();
opt2 = GBreader.readLine(); //GuestBookData.read();
opt3 = GBreader.readLine(); //GuestBookData.read();
opt4 = GBreader.readLine(); //GuestBookData.read();

GBreader.close();
}
catch(IOException e)
{
// If file does not exist, create the Guestbook from scratch.
opt1 = opt2 = opt3 = opt4 = null;

}

// Write comments to the text file
try
{
FileWriter GuestBookData = new FileWriter(new File(file_name));
BufferedWriter GBwriter = new BufferedWriter(GuestBookData);

GBwriter.write(opt1); GBwriter.write('\n');
GBwriter.write(opt2); GBwriter.write('\n');
GBwriter.write(opt3); GBwriter.write('\n');
GBwriter.write(opt4); GBwriter.write('\n');

//GuestBookData.write(opt1); GuestBookData.write('\n');
//GuestBookData.write(opt2); GuestBookData.write('\n');
//GuestBookData.write(opt3); GuestBookData.write('\n');
//GuestBookData.write(opt4); GuestBookData.write('\n');


GuestBookData.close();
}
catch(IOException e)
{
// Ooops a problem with the file!
}
out.println( "<br> Total Option 1: " + (opt1) );
out.println( "<br> Total Option 2: " + (opt2) );
out.println( "<br> Total Option 3: " + (opt3) );
out.println( "<br> Total Option 4: " + (opt4) );
writeResults(value, out, "/tmp/username_guestbook.txt");
// writeResults(value, out, "guestbook.txt");
}

}