// File: SampleServlet.java // Simple servlet example that tells user how many times it has been invoked // and the last time it was invoked. import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; //import java.math.*; /** * Sample Servlet * * This servlet returns "Hello World" and the number of times * the servlet has been requested. */ public class SampleServlet extends HttpServlet { int numRequests = 0; //number of times servlet requested String lastRequest = (new java.util. Date()).toString(); //last request Properties prop = null; /** * init() is called when the servlet is first loaded. Use this * method to initialize resources. * * This method reads (from a properties file) the number of * times the servlet has been requested and initializes the * numRequests variable. */ public void init() throws ServletException { prop = new Properties(); //create new Properties object try { //create a file object pointing to the properties file File file = new File("SampleServlet.properties"); //determine if the properties file exists if (file.exists()) { //get an input stream to the properties file FileInputStream fileIn = new FileInputStream(file); prop.load(fileIn); //load the properties object from file //initialize the numRequests variable, default to zero numRequests = Integer.parseInt( prop.getProperty("RequestCount", "0")); //initialize lastRequest, default to current date/time lastRequest = prop.getProperty("LastRequest", (new java.util.Date()).toString()); } else { //properties file doesn't exist, use default values } } catch (Exception e) { //if unable to read file, use default values } } /** * service() is called with each client request. */ public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //set MIME type for HTTP header to HTML response.setContentType("text/html"); //get a handle to the output stream PrintWriter out = response.getWriter(); //send HTML response to client out.println(""); out.println("
This servlet has been requested " +
numRequests++ + " time(s).");
out.println("
Last requested at " + lastRequest + ".");
out.println("");
lastRequest = (new java.util.Date()).toString(); //update last request
out.close(); //always close the output stream
connection.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* getServletInfo() allows the server to query this servlet for
* information regarding its name, version, and brief
* description.
*/
public String getServletInfo()
{
return "Sample Servlet version 1.1";
}
/**
* Destroy is called before the server unloads the servlet. Use
* this method to release any resources held by the servlet and
* other housekeeping chores.
*
* This method stores (in a properties file) the number of
* times the servlet has been requested.
*/
public void destroy()
{
try
{
//create a file object pointing to the properties file
File file = new File("SampleServlet.properties");
//get an output stream to the properties file
FileOutputStream fileOut = new FileOutputStream(file);
//store the request count in the properties object
prop.put("RequestCount", Integer.toString(numRequests));
//store the last request date/time in the properties object
prop.put("LastRequest", lastRequest);
//write the properties object data to the properties file
prop.store(fileOut, "SampleServlet Storage File");
}
catch (Exception e)
{
//if there is an error writing to file, ignore it
}
}
}