Yurttas/PL/OOL/Java/F/08/03/01/02/SampleServlet00.java

From ZCubes Wiki
Jump to navigation Jump to search
  1/**
  2 *  Copyright(C) 2000
  3 *  All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
  4 *
  5 *  Permission to use, copy, modify, and distribute this
  6 *  software and its documentation for EDUCATIONAL purposes
  7 *  and without fee is hereby granted provided that this
  8 *  copyright notice appears in all copies.
  9 *
 10 *   @date   : June 1, 2000.
 11 *   @author : Salih Yurttas.
 12 */
 13
 14
 15import javax.servlet.*;
 16import javax.servlet.http.*;
 17
 18import java.io.*;
 19
 20import java.util.*;
 21
 22/**
 23 *  Sample Servlet
 24 *
 25 *  This servlet returns "Hello World" and the number of times
 26 *  the servlet has been requested.
 27 */
 28
 29
 30public class SampleServlet00 extends HttpServlet {
 31
 32  int numRequests = 0; //number of times servlet requested
 33  String lastRequest = (new Date()).toString(); //last request
 34  Properties prop = null;
 35
 36  /**
 37   *  init() is called when the servlet is first loaded. Use this
 38   *  method to initialize resources.
 39   *
 40   *  This method reads (from a properties file) the number of
 41   *  times the servlet has been requested and initializes the
 42   *  numRequests variable.
 43   */
 44
 45  public void init()
 46    throws ServletException
 47  {
 48    prop = new Properties(); //create new Properties object
 49
 50    try {
 51      //create a file object pointing to the properties file
 52      File file = new File("SampleServlet.properties");
 53
 54      //determine if the properties file exists
 55      if(file.exists()) {
 56        //get an input stream to the properties file
 57        FileInputStream fileIn = new FileInputStream(file);
 58
 59        prop.load(fileIn); //load the properties object from file
 60
 61        //initialize the numRequests variable, default to zero
 62        numRequests = Integer.parseInt(prop.getProperty("RequestCount", "0"));
 63
 64        //initialize lastRequest, default to current date/time
 65        lastRequest = prop.getProperty("LastRequest",
 66                                       (new Date()).toString());
 67      }
 68      else {
 69        //properties file doesn't exist, use default values
 70      }
 71    }
 72    catch(Exception e) {
 73      //if unable to read file, use default values
 74    }
 75  }
 76
 77  /**
 78   *  service() is called with each client request.
 79   */
 80
 81  public void service(HttpServletRequest request,
 82                      HttpServletResponse response)
 83    throws ServletException,
 84    IOException
 85  {
 86    //set MIME type for HTTP header to HTML
 87    response.setContentType("text/html");
 88
 89    //get a handle to the output stream
 90    PrintWriter out = response.getWriter();
 91
 92    //send HTML response to client
 93    out.println("<HTML>");
 94    out.println("<HEAD><TITLE>Sample Servlet</TITLE></HEAD>");
 95    out.println("<BODY>");
 96    out.println("<H1>Hello World!</H1>");
 97    out.println("<P>This servlet has been requested " +
 98      numRequests++ + " time(s).");
 99    out.println("<BR>Last requested at " + lastRequest + ".");
100    out.println("</BODY></HTML>");
101
102    lastRequest = (new Date()).toString(); //update last request
103
104    out.close(); //always close the output stream
105  }
106
107  /**
108   *  getServletInfo() allows the server to query this servlet for
109   *  information regarding its name, version, and brief
110   *  description.
111   */
112
113  public String getServletInfo() {
114    return "Sample Servlet version 1.0";
115  }
116
117  /**
118   *  Destroy is called before the server unloads the servlet. Use
119   *  this method to release any resources held by the servlet and
120   *  other housekeeping chores.
121   *
122   *  This method stores (in a properties file) the number of
123   *  times the servlet has been requested.
124   */
125
126  public void destroy() {
127    try {
128      //create a file object pointing to the properties file
129      File file = new File("SampleServlet.properties");
130
131      //get an output stream to the properties file
132      FileOutputStream fileOut = new FileOutputStream(file);
133
134      //store the request count in the properties object
135      prop.put("RequestCount", Integer.toString(numRequests));
136
137      //store the last request date/time in the properties object
138      prop.put("LastRequest", lastRequest);
139
140      //write the properties object data to the properties file
141      prop.store(fileOut, "SampleServlet Storage File");
142    }
143    catch(Exception e) {
144      //if there is an error writing to file, ignore it
145    }
146  }
147
148}