I am writing a hit counter in JSP, for my coursework. I have write the code, and there is not error and its working, but the problem is that: if the user have open the website and try to use different page, whenever that the user goes back to the home page the counter still is adding a number, how can I restrict this part? shall restrict it with session? this is my code :
<jsp:useBean id="counter" scope="application" class="counter.CounterBean" />
The current count for the counter bean is:
<jsp:setProperty name="counter" property="coun" value="1"></jsp:setProperty>
<%
counter.saveCount();
int _numberofvisitors=counter.getVisitorsNumber();
out.println(_numberofvisitors);
%>
Bean:
package counter;
import java.sql.*;
import java.sql.SQLException;
public class CounterBean implements java.io.Serializable {
int coun = 0;
public CounterBean() {
database.DatabaseManager.getInstance().getDatabaseConnection();
}
public int getCoun() {
return this.coun;
}
public void setCoun(int coun) {
this.coun += coun;
}
public boolean saveCount() {
boolean _save = false;
database.SQLUpdateStatement sqlupdate = new database.SQLUpdateStatement("counter", "hitcounter");
sqlupdate.addColumn("hitcounter", getCoun());
if (sqlupdate.Execute()) {
_save = true;
}
return _save;
}
public int getVisitorsNumber() throws SQLException {
int numberOfVisitors = 0;
if (database.DatabaseManager.getInstance().connectionOK()) {
database.SQLSelectStatement sqlselect = new database.SQLSelectStatement("counter", "hitcounter", "0");
ResultSet _userExist = sqlselect.executeWithNoCondition();
if (_userExist.next()) {
numberOfVisitors = _userExist.getInt("hitcounter");
}
}
return numberOfVisitors;
}
}