package examples.servlets; /* * @(#)SurveyServlet.java * * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; /** * This example servlet takes input from a form and writes it out to a simple * text file. After the results are written to the file, the servlet returns a * "thank you" to the user. This example has been modified from the SurveyServlet * that is distributed with the JSDK2.0 - see note below. * *
Note: This servlet has been modified from the SurveyServlet that is
* distributed with the JSDK2.0 so that it does not implement the
* SingleThreadModel. WebLogic does not support the SingleThreadModel. This model
* is of limited value, and the original JavaSoft example illustrated here is
* bogus with respect to ensuring that output to the text file is guarded against
* simultaneous access from multiple servlet threads. Although the
* SingleThreadModel does prevent a sevlets engine from invoking a servlet
* instance with multiple threads simultaneously, the spec describes that the server may
* initialize any number of servlet instances. In this example these servlet
* instances would all access and write to the same file - possibly
* simultaneously, regardless of the SingleTreadModel interface. See the note in the
* WebLogic servlet developer's guide for more details.
*
* @author Adapted from the JSDK2.0 by BEA Systems, Inc.
*/
public class SurveyServlet extends HttpServlet
{
String resultsDir;
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
resultsDir = getInitParameter("resultsDir");
if (resultsDir == null) {
Enumeration initParams = getInitParameterNames();
System.err.println("The init parameters were: ");
while (initParams.hasMoreElements()) {
System.err.println(initParams.nextElement());
}
System.err.println("Should have seen one parameter name");
throw new UnavailableException (this,
"Not given a directory to write survey results!");
}
}
/**
* Write survey results to output file in response to the POSTed
* form. Write a "thank you" to the client.
*/
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
// first, set the "content type" header of the response
res.setContentType("text/html");
//Get the response's PrintWriter to return text to the client.
PrintWriter toClient = res.getWriter();
try {
//Open the file for writing the survey results.
String surveyName = req.getParameterValues("survey")[0];
FileWriter resultsFile = new FileWriter(resultsDir
+ System.getProperty("file.separator")
+ surveyName + ".txt", true);
PrintWriter toFile = new PrintWriter(resultsFile);
// Get client's form data & append it to the file
toFile.println("