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

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException

Multithreading in java with example