import mixer.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Static extends HttpServlet{ File file = null; private static String htmlStatic = null; /* Skapar filen count och skriver vŠrdet 0 om den inte redan finns LŠser in filen Static.html */ public void init() throws ServletException{ try{ file = new File(getServletContext().getRealPath("count")); if (file.createNewFile()) writeValue(file, 0); }catch (IOException ignore){} if (htmlStatic == null) { htmlStatic = Mixer.getContent(new File(getServletContext().getRealPath("Static.html"))); } } public void destroy(){ super.destroy(); } // Returnerar vŠrdet frŚn filen count int readValue(File file)throws IOException{ BufferedReader infile = new BufferedReader(new FileReader(file)); int tmp = Integer.parseInt(infile.readLine()); infile.close(); return tmp; } // Skriver ett nytt vŠrde till filen count void writeValue(File file, int value)throws IOException{ PrintWriter outfile = new PrintWriter(new FileWriter(file)); outfile.println(value); outfile.close(); } /* …kar vŠrdet pŚ filen count ersŠtter texten ===antal=== med det nya vŠrdet pŚ count och skriver ut html sidan */ public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException { Mixer mix = new Mixer(htmlStatic); int count; res.setContentType("text/html"); PrintWriter out = res.getWriter(); synchronized (this){ count = readValue(file); writeValue(file, ++count); } mix.add("===antal===", Integer.toString(count)); out.println(mix.getMix()); } }