Skip to main content

how to count the page views by using JSP

To count no.of page's are viewed by user in your application is a challenge in real time application. In this page we will see how to count the page views when user has viewed the page.

          Here, we will do one example how many times user has visited each page in our application. In this post we will come to know, how to count the total application page views as well as individually each pages count.

          Let us assume my web application name is "Page Counter". For creating the web application in eclipse IDE, Click here. This Page Counter contains three pages, assume that are the different pages in an application. But, there is the link between each other files for navigating from one page to another. Also it will show the total pages view count and individual page view count in all the pages. Let us see, below is my first jsp file,

          You can declare the below content inside the <body> tag. Rest of the content in every page will be same only, it also generated by IDE.

firstFile.jsp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<%
     Integer allPagesCount = (Integer) application
              .getAttribute("allPagesCount");
     if (allPagesCount == null) {
          allPagesCount = 1;
     } else {
          allPagesCount += 1;
     }

     Integer firstPageCounter = (Integer) application
              .getAttribute("firstPageCounter");

     if (firstPageCounter == null) {
          out.println("Hai, Welcome to first page");
          firstPageCounter = 1;
     } else {
          out.println("Welcome back to first page");
          firstPageCounter += 1;
     }
     application.setAttribute("firstPageCounter", firstPageCounter);
     application.setAttribute("allPagesCount", allPagesCount);
%>

<center>
<p style="color: maroon;"><b>First Page total no.of visits : </b><%=firstPageCounter%></p>
<br />
</center>
<center>
<p style="color: blue;"><b>All Pages total no.of visits : </b><%=allPagesCount%></p>
<br />
</center>
<div>
<p align="right"><a href='/PageCounter/secondPage.jsp'>Second
Page</a></p>
<p align="left"><a href='/PageCounter/thirdPage.jsp'>Third Page</a></p>
</div>

        Like the firstFile.jsp, Here we will one more JSP name as secondFile.jsp and declare the below content inside the <body> tag.

secondFile.jsp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<%
     Integer allPagesCount = (Integer) application
              .getAttribute("allPagesCount");
     if (allPagesCount == null) {
          allPagesCount = 1;
     } else {
          allPagesCount += 1;
     }

     Integer secondPageCounter = (Integer) application
              .getAttribute("secondPageCounter");
     if (secondPageCounter == null ) {
          out.println("Hai, Welcome to second page");
          secondPageCounter = 1;
     } else {
          out.println("Welcome back to second page");
          secondPageCounter += 1;
     }
     application.setAttribute("secondPageCounter", secondPageCounter);
     application.setAttribute("allPagesCount", allPagesCount);
%>

<center>
<p style="color: maroon;"><b>Second page total no.of visits: </b><%=secondPageCounter%></p>
<br />
</center>
<center>
<p style="color: blue;"><b>All pages total no.of visits: </b><%=allPagesCount%></p>
<br />
</center>
<div>
<p align="right""><a href='/PageCounter/thirdPage.jsp'>Third
Page</a></p>
<p align="left""><a href='/PageCounter/firstPage.jsp'>First Page</a></p>
</div>

        Like secondFile.jsp, here we will create one more jsp called thirdFile.jsp and declare below content inside the <body> tag.

thirdFile.jsp:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<%
     Integer allPagesCount = (Integer) application
              .getAttribute("allPagesCount");
     if (allPagesCount == null) {
          allPagesCount = 1;
     } else {
          allPagesCount += 1;
     }

     Integer thirdPageCounter = (Integer) application
              .getAttribute("thirdPageCounter");
     if (thirdPageCounter == null ) {
          out.println("Hai, Welcome to third page");
          thirdPageCounter = 1;
     } else {
          out.println("Welcome back to third page");
          thirdPageCounter += 1;
     }
     application.setAttribute("thirdPageCounter", thirdPageCounter);
     application.setAttribute("allPagesCount", allPagesCount);
%>

<center>
<p style="color: maroon;"><b>Third page total no.of visits: </b><%=thirdPageCounter%></p>
<br />
</center>
<center>
<p style="color: blue;"><b>All pages total no.of visits: </b><%=allPagesCount%></p>
<br />
</center>
<div>
<p align="right"><a href='/PageCounter/firstPage.jsp'>First Page</a></p>
<p align="left""><a href='/PageCounter/secondPage.jsp'>Second
Page</a></p>
</div>

          All jsp files you should be created inside the WebContent folder in the project structure. This web content folder it will creates auto matically by the IDE.

          Once jsp pages creation and declaration is done. Then we will declare which is the welcome-file by by defining the <welcome-file> tag which will present inside the web.xml file. The below code you can replace into your web.xml file which will present under the WEB-INF folder.

Web.xml:

1
2
3
4
<display-name>Your application name</display-name>
  <welcome-file-list>
    <welcome-file>firstPage.jsp</welcome-file>
  </welcome-file-list>

          In every file we are defining the same procedure. There mainly we are using the keyword called application which will be the scope of the session. Session contains the methods for setting values in one file and we will get that values at any file using same session scope.

              •  setAttribute(string name, value)
              •  getAttrubute(name)

          where you are setting value to application there you need to pass the name and passing value. Here, passing name will be helpful to get the value by passing name inside the getAttribute(name) method.

Note:
          In side the setAttibute() passing name and getAttribute() passing names should be same. Otherwise, it will get null. If your passing name inside the getAttribute() method is matching with another setAttribute() name it will fetches that value to here. So make sure name will be unique.

          Once everything is done, you can run the application in any of the  servers. Like, Tomcat, Jboss, etc.

Do you know?

Comments

Popular posts from this blog

JNDI configuration for Tomcat 9 with Oracle

In this article, I am going to place the required source code to get data from the table by using the JNDI configuration. Below are the environment details that I have configured currently. Windows - 7 Oracle - 10g Tomcat - 9 JDK - 8 Eclipse Oxygen Ojdbc6 jar required First, we need to create the Dynamic Web Project. If you don't know how to do <Click Here>. I have faced a lot of issues before getting the output like 405, No driver class to load, etc. I am using JSP & Servlets in the current explanation. Before started writing the application logic, we need to do the below configuration in the installed tomcat directory. Place OJDBC6.jar in the Tomcat LIB directory. Add this Resource under <GlobalNamingResources> in Server.xml file which is present under the conf directory in Tomcat. < Resource name = "jdbc/myoracle" global= "jdbc/myoracle" auth = "Container" type= "javax.sql.DataSource" driverClass...

Prime, Fibonacci and Factorial number with example in java

Prime number, Fibonacci series and Factorial number programs are most commonly asked questions in interview. Read this article to know what is and how to write programs for prime number, fibonacci series and factorial number. Prime Number: prime number is natural number greater than 1 that has no positive divisor other than 1 and itself. A natural number greater than 1 is not a prime number, is called Composite number . For example, 7 is a prime number. Because it can divide with 1 and 7 only. Where as 8 is composite number. Since it has the divisor 2 and 4 in addition to the 1 and 8. The below example represents the finding the passing number is prime number or not. If the passing number is prime number it will print true otherwise it will print false. package com . javatbrains . practice ; public class PrimeNumber { public boolean isPrimeNumber ( int number ) { if ( number <= 1 ) return false ; // There's only one ...

JVM, JRE and JDK in Java

JVM, JRE and JDK are the most basic common concepts to know in java. These are the basic features to understand how Java architecture works? JVM stands for Java Virtual Machine, which doesn't have any physical directories created in java installation. JRE stands for Java Runtime Environment, which creates the directory under Java installation path and also present in JDK. JDK stands for Java Development Kit, which creates the directory in Java installation path and also it has it's own JRE. Since we have already learn that Java is platform independent means if we have implemented any of the java class in one environment, it will be executed in any other environment and provides the same output. But, JVM, JRE and JDK all are platform dependent . So that, for windows, linux, unix, mac, solaris..etc has it's own JVM, JRE and JDK. One will be not compatible with other environments. While installing the Java, we might come to know a bit about JRE and JDK. But, JVM is the other...