2010-04-18 82 views
0

我在JSP中寫了一個計數器,用於我的課程。我寫了代碼,並沒有錯誤和它的工作,但問題是: 如果用戶打開網站並嘗試使用不同的頁面,每當用戶返回主頁時,計數器仍然是添加一個數字,我該如何限制這部分?應該用會話來限制它? 這是我的代碼:如何限制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; 
    } 
} 
+0

目前還不清楚你想算什麼:1獨特的訪問者每次不管所訪問的頁面的?你想基於會話還是基於獨特的IP來計算這個值?或者您想每次訪問每位訪問者的唯一瀏覽量?等 – BalusC 2010-04-18 14:55:46

回答

2

更改這部分代碼:

<% 
counter.saveCount(); 
int _numberofvisitors=counter.getVisitorsNumber(); 
out.println(_numberofvisitors); 
%> 

<% 
if (session.isNew()) { 
    counter.saveCount(); 
} else { 
    counter.setCoun(-1); 
} 
int _numberofvisitors=counter.getVisitorsNumber(); 
out.println(_numberofvisitors); 
%> 

希望這有助於。

UPDATE:順便說一下,最好爲Counter類的方法選擇更好的名稱。首先,將setCoun更改爲setCount。另外,setter方法通常只是將傳遞給它的值賦給它的相關字段。如果要增加coun的值,請將方法名稱更改爲addCount。然後遞增count值,如:

<jsp:setProperty name="counter" property="coun" value="${1 + counter.coun}"></jsp:setProperty> 
+0

非常感謝你的工作 – user261002 2010-04-18 15:09:57

+0

謝謝你的建議祝福你 – user261002 2010-04-18 15:12:41

+0

不客氣。 :)祝你的作業好運。 :) – Behrang 2010-04-18 15:37:42

1
<%@page import="java.io.*,java.util.*" %> 

<html> 
<head> 
<title>Applcation object in JSP</title> 
</head> 
<body> 
<% 


Integer hitsCount=(Integer)application.getAttribute("hitcount"); 
int m; 
     if(hitsCount==null) 
    { 

      m=1; 

     hitsCount =Integer.valueOf(m); 
        application.setAttribute("hitcount", hitsCount); 
} 
else 
    { 

     hitsCount=(Integer)application.getAttribute("hitcount"); 
     m=hitsCount.intValue()+1; 
     hitsCount= Integer.valueOf(m); 
     application.setAttribute("hitcount", hitsCount); 

    } 



%> 


<center> 
<p>Total number of visits:<%=hitsCount.intValue()%> </p> 
</center> 
</body> 
</html>