我正在为我的课程作业在 JSP 中编写一个计数器。我已经编写了代码,并且没有错误及其工作,但问题是:如果用户打开网站并尝试使用不同的页面,每当用户返回主页时,计数器仍在添加一个数字,我该如何限制这部分?应该用会话限制它吗?这是我的代码:
<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);
%>
豆:
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;
}
}