Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 737031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:40:25+00:00 2026-05-14T07:40:25+00:00

I am writing a hit counter in JSP, for my coursework. I have write

  • 0

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;
    }
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:40:26+00:00Added an answer on May 14, 2026 at 7:40 am

    Change this part of the code:

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

    To

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

    Hope this helps.

    UPDATE: By the way, it’s better to choose better names for the methods of your Counter class. First of all, change setCoun to setCount. Besides, a setter method usually just assigns the value passed to it to its associated field. If you want to increment the value of coun, change the method name to addCount. Then increment the count value like:

    <jsp:setProperty name="counter" property="coun" value="${1 + counter.coun}"></jsp:setProperty>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

when you hit a serious compilation error while writing JSP pages and running under
I've begun writing a game using XNA Framework and have hit some simple problem
I'm writing some Objective-C code and I've frequently hit the situation where I have
I'm working on the UI for an application I'm writing but I've hit a
I'm writing a simple shopping cart application and have hit a road block. Currently
I'm currently writing a Rails app, and hit a somewhat strange quirk. I have
I'm writing an integration between Salesforce.com and another service, and I've hit a problem
Is there any performance hit for writing a function such that local var statements
I am writing a GLPaint-esque drawing application for the iPad, however I have hit
I'm writing a simple web application and have hit a stumbling block of sorts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.