How to Write a Hit Counter in JSP

A hit counter is an application that shows the amount of times a particular process occurs. This can be the number of times a Web page is viewed, a video is played or any other repetitive action that is user- or computer-driven occurs. The two primary types of hit counters are those that increment and those that decrement. Creating a hit counter in Java Server Pages (JSP) is a common task for Web masters.

Instructions

    • 1

      Create a table in MySQL with the "Query Manager" by choosing the "Create New Table" menu option.

    • 2

      Start a procedure for the table created in Step 1 by using the following SQL code:

      "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[counter]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
      drop table [dbo].[counter]
      GO
      CREATE TABLE [dbo].[counter] (
      [hit] [int] NOT NULL
      ) ON [PRIMARY]
      GO
      INSERT [dbo].[counter] (hit) VALUES (1)
      GO
      if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[webcounter]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
      drop procedure [dbo].[webcounter]
      GO
      SET QUOTED_IDENTIFIER OFF
      GO
      SET ANSI_NULLS OFF
      GO
      CREATE PROCEDURE dbo.webcounter
      AS
      BEGIN
      SET NOCOUNT ON
      DECLARE @hits INT
      update counter set hit = hit + 1
      SELECT hit from counter
      END
      GO
      SET QUOTED_IDENTIFIER OFF
      GO
      SET ANSI_NULLS ON
      GO"

    • 3

      Write the script that will import the counter code for the website to display the requisite information. An example of script code that will display the JSP hit counter is:

      "<%@ page import="java.sql.*" %>
      <%
      Connection ocon = null;
      Class.forName("com.inet.tds.TdsDriver").newInstance();
      ocon = DriverManager.getConnection("jdbc:inetdae7:localhost", "sa", "");
      ocon.setCatalog( "dotnet" );

      Statement stmtt = ocon.createStatement();
      ResultSet rc = stmtt.executeQuery("EXEC dbo.webcounter");
      rc.next();
      int pages = rc.getInt(1);
      out.println("Hits : " + pages + "
      ");
      %>"

    • 4

      Include the hit counter script code on the HTML or PHP Web page that you desire to display the JSP count by copy-and-pasting the script from Step 3 to in between the opening and closing body tag on your Web page.

Related Searches:

References

Resources

Comments

You May Also Like

Related Ads

Featured