Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use a hit counter with a session var

Status
Not open for further replies.

charlotte49er

Programmer
Nov 17, 2001
25
US
Okay, I am a newbie here so be gentle...What I want to do is have a hit counter that uses a session variable so that the hit counter is increased only if the visitor hasn't been around for awhile. My code below works fine as long as I comment out the code line relating the session. However, when I try to implement the conditional based on the session, it craps out on me. Can someone give any suggestions as to why this won't work or maybe some sample code? Thanks.

Code:
<%@page contentType=&quot;text/html&quot;%>
<%@page import = &quot;java.util.*&quot; %>
<%@page import = &quot;java.io.*&quot; %>

<html>
<head>
    <title>JSP Page</title>
</head>
<body>

<%
Integer accessCount = (Integer)session.getAttribute(&quot;accessCount&quot;);

if (accessCount == null) {        
        // Create a File OutputStream
        FileOutputStream fos = new FileOutputStream(&quot;test.dat&quot;);
        
        // Create a DataOutputStream and chain it to the FileOutputStream
        DataOutputStream dos = new DataOutputStream(fos);

        // Write values to the file
        dos.writeDouble(Math.sqrt(50));

        // Close the output stream
        dos.close();
        fos.close();

        // Create a FileInputStream 
        FileInputStream fis = new FileInputStream(&quot;test.dat&quot;);

        // Create DataInputStream which is chained to the FileInputStream
        DataInputStream dis = new DataInputStream(fis);

        // Read the values
        out.println(dis.readDouble());
        
        // Close the input streams
        dis.close();
        fis.close();
}

session.setAttribute(&quot;accessCount&quot;, accessCount);
%>

</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top