?? servletcounter.java
字號(hào):
package com.examples.servlet;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;public class ServletCounter extends HttpServlet { HashMap counts; // A hash table: maps ServletCounter names to counts File countfile; // The file that counts are saved in long saveInterval; // How often (in ms) to save our state while running? long lastSaveTime; // When did we last save our state? // This method is called when the web server first instantiates this // servlet. It reads initialization parameters (which are configured // at deployment time in the web.xml file), and loads the initial state // of the ServletCounter variables from a file. public void init() throws ServletException { ServletConfig config = getServletConfig(); try { // Get the save file. countfile = new File(config.getInitParameter("countfile")); // How often should we save our state while running? saveInterval = Integer.parseInt(config.getInitParameter("saveInterval")); // The state couldn't have changed before now. lastSaveTime = System.currentTimeMillis(); // Now read in the count data loadState(); } catch(Exception e) { // If something goes wrong, wrap the exception and rethrow it throw new ServletException("Can't init ServletCounter servlet: " + e.getMessage(), e); } } // This method is called when the web server stops the servlet (which // happens when the web server is shutting down, or when the servlet is // not in active use.) This method saves the counts to a file so they // can be restored when the servlet is restarted. public void destroy() { try { saveState(); } // Try to save the state catch(Exception e) {} // Ignore any problems: we did the best we could } // These constants define the request parameter and attribute names that // the servlet uses to find the name of the ServletCounter to increment. public static final String PARAMETER_NAME = "counter"; public static final String ATTRIBUTE_NAME = "com.davidflanagan.examples.servlet.Counter.counter"; /** * This method is called when the servlet is invoked. It looks for a * request parameter named "counter", and uses its value as the name of * the ServletCounter variable to increment. If it doesn't find the request * parameter, then it uses the URL of the request as the name of the * counter. This is useful when the servlet is mapped to a URL suffix. * This method also checks how much time has elapsed since it last saved * its state, and saves the state again if necessary. This prevents it * from losing too much data if the server crashes or shuts down without * calling the destroy() method. **/ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { // Get the name of the ServletCounter as a request parameter String counterName = request.getParameter(PARAMETER_NAME); // If we didn't find it there, see if it was passed to us as a // request attribute, which happens when the output of this servlet // is included by another servlet if (counterName == null) counterName = (String) request.getAttribute(ATTRIBUTE_NAME); // If it wasn't a parameter or attribute, use the request URL. if (counterName == null) counterName = request.getRequestURI(); Integer count; // What is the current count? // This block of code is synchronized because multiple requests may // be running at the same time in different threads. Synchronization // prevents them from updating the counts hashtable at the same time synchronized(counts) { // Get the ServletCounter value from the hashtable count = (Integer)counts.get(counterName); // Increment the counter, or if it is new, log and start it at 1 if (count != null) count = new Integer(count.intValue() + 1); else { // If this is a ServletCounter we haven't used before, send a message // to the log file, just so we can track what we're counting // Start counting at 1! count = new Integer(1); } // Store the incremented (or new) ServletCounter value into the hashtable counts.put(counterName, count); // Check whether saveInterval milliseconds have elapsed since we // last saved our state. If so, save it again. This prevents // us from losing more than saveInterval ms of data, even if the // server crashes unexpectedly. if (System.currentTimeMillis() - lastSaveTime > saveInterval) { saveState(); lastSaveTime = System.currentTimeMillis(); } } // End of synchronized block // Finally, output the ServletCounter value. Since this servlet is usually // included within the output of other servlets, we don't bother // setting the content type. PrintWriter out = response.getWriter(); out.print(count); } // included in pages that are loaded with POST requests public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException { doGet(request, response); } // Save the state of the counters by serializing the hashtable to // the file specified by the initialization parameter. void saveState() throws IOException { ObjectOutputStream out = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream(countfile))); out.writeObject(counts); // Save the hashtable to the stream out.close(); // Always remember to close your files! } // Load the initial state of the counters by de-serializing a hashtable // from the file specified by the initialization parameter. If the file // doesn't exist yet, then start with an empty hashtable. void loadState() throws IOException { if (!countfile.exists()) { counts = new HashMap(); return; } ObjectInputStream in = null; try { in = new ObjectInputStream( new BufferedInputStream(new FileInputStream(countfile))); counts = (HashMap) in.readObject(); } catch(ClassNotFoundException e) { throw new IOException("Count file contains bad data: " + e.getMessage()); } finally { try { in.close(); } catch (Exception e) {} } }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -