You can enhance the servlet introduced in
Segment 1048 by adding statements that write
information to a file. Note that the new version implements the
SingleThreadModel
interface, as explained in
Segment 1050.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ProcessCriticFormServlet extends HttpServlet
implements SingleThreadModel {
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Process arguments
String title = request.getParameter("title");
String script = request.getParameter("script");
String acting = request.getParameter("acting");
String direction = request.getParameter("direction");
// Writing preparation
response.setContentType("text/html");
PrintWriter webWriter = response.getWriter();
// Writing HTML page goes here
// File output
String user = request.getRemoteUser();
String host = request.getRemoteHost();
FileOutputStream stream
= new FileOutputStream("VotingData.txt", true);
PrintWriter fileWriter = new PrintWriter(stream);
if (user != null) {
fileWriter.println("Voter: " + user + " at " + host);
}
else {fileWriter.println("Voter at " + host);}
fileWriter.println("Evaluated \"" + title + "\" stipulating:");
if(script != null) {fileWriter.println("Script: " + script);}
if(acting != null) {fileWriter.println("Acting: " + acting);}
if(direction != null) {
fileWriter.println("Direction: " + direction);
}
fileWriter.println("");
fileWriter.flush();
stream.close();
}}