mercredi 30 mai 2018

How to work with files in JSP

I need to make a counter of visits in JSP, so i just read the number of visits from a file and then write to it the new value. But the following code doesn't work. Though it shows the date and time correctly.
What's the problem? Or maybe there is a better solution to make a counter?

<div style="width: auto; height: 60px; clear: both; margin-top: 15px; color: white; ">
    <span style="display: inline-block; border: 1px solid white;  padding: 15px; float: left; ">
        Current date and time: <%= new java.util.Date()%>       
    </span>
    <span style="display: inline-block; border: 1px solid white;  padding: 15px; float: right; color: white;">
    <%
        try {

            DataInputStream reader = new DataInputStream(new FileInputStream("data.bin"));
            int counter = reader.readInt();
            counter++;
            out.println("Visits: " + counter);
            reader.close();

            DataOutputStream writer = new DataOutputStream( new FileOutputStream("data.bin"));
            writer.writeInt(counter);
            writer.close();

        } catch (Exception e) { System.out.println("Error!"); }
    %>
    </span>
</div>




Aucun commentaire:

Enregistrer un commentaire