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 7854347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:02:23+00:00 2026-06-02T20:02:23+00:00

I tried to use application object to record how many visitors have viewed this

  • 0

I tried to use application object to record how many visitors have viewed this page,but after I refreshed the page I closed the browser,and when I open the browser again to view this page,the record back to the number I started to refresh.I don’t know why?

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<html>
    <body>
        <%
            Integer count;
            synchronized (application) {
                count = (Integer) application.getAttribute("count");
                if(count == null)
                    count = new Integer(0);
                count = new Integer(count.intValue() + 1);
                application.setAttribute("count", count);
            }
        %>
        This page has been visited <%= count.intValue() %> times!
    </body>
</html>
  • 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-06-02T20:02:27+00:00Added an answer on June 2, 2026 at 8:02 pm

    Why have synchronized? Why dont you just use a global variable (ie java static)?

    You don’t have to worry about threads on the web-server. It should handle that.

    A global variable on the server will be the same for all threads.

    Sample here http://www.tutorialspoint.com/jsp/jsp_hits_counter.htm

    which says to use :

    application.setAttribute(String Key, Object Value);
    

    then get it with ..

    application.getAttribute(String Key);
    

    An example:

    <%
        Integer hitsCount = (Integer)application.getAttribute("hitCounter");
        if( hitsCount ==null || hitsCount == 0 ){
           /* First visit */
           out.println("Welcome to my website!");
           hitsCount = 1;
        }else{
           /* return visit */
           out.println("Welcome back to my website!");
           hitsCount += 1;
        }
        application.setAttribute("hitCounter", hitsCount);
    %>
    

    with

    <p>Total number of visits: <%= hitsCount%></p>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to use object expression to extend the IDelegateEvent, but in fsi there
I tried to use this code but it doesn't work. http://developer.apple.com/library/ios/#samplecode/PageControl/Introduction/Intro.html Ld /Users/waitonza/Library/Developer/Xcode/DerivedData/Dr_Ngoo-aanknxmuodcgjicaigxevljxokeq/Build/Products/Debug-iphonesimulator/Dr Ngoo.app/Dr
I have spent the whole day trying to make my application use threads but
I tried to use an image file in my HTML template. But when I
I tried to use this: private void CreateDevice() { _playbackDevice = new WaveOut(); _playbackDevice.PlaybackStopped
I tried to use __declspec(dllexport) to export functions in my DLL, but it doesn't
In my application I'm using LDAP authentication. But i'm also have 2 remote services
I want to use ASP.NET Application Services but configure it to use a different
I tried to use the WebBrowser control in an ASP .NET application: public BrowserForm()
I have a small file explorer application [WINFORMS], and i use ListView Control to

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.