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

Multithreading in java with example

Multithreading  is one of the most important concept in core java. In this article we will learn what is multithreading? , what is the use of it? and What is the use of Synchronization and when to use it?  with detailed examples. At a time, two or more threads are accessing the same object is called as Multithreading  in Java .  First, we will create two threads for two objects. It is also possible to run two or more threads on a single class object. In this case, there is a possibility to get unreliable results. If the two threads are perform same task, then they need same object to be executed each time. For your better understanding, take an example of any reservations like, railway, movie ticket booking,etc. Let us think only one berth is available in a train and two passengers are asking for that berth. The first person has sent a request to allocate that ticket/berth to him. At the same time, the second person also sent a request to allocate that ...

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...

Git installation for AngularJS 2 in Windows 10

Download Git latest version from https://git-scm.com/downloads or you click on the below link to download directly for windows https://git-scm.com/download/win . Once download completes, click on executable file to start installation process and choose Yes to allow the software installation in windows 10. Click on Next button to continue further installation. Browse the isntallation directory and click on Next button to continue. Select the list of components which you want to be installed and click on Next button to proced further installation. Type the shortcut name for Start menu and click on Next button. Select how you want to use the Git and click on Next button. For Windows no need to change anything, let it be the default one. Choose the Use the OpenSSL library and click on Next button. Select how should Git treat line ending in text files and click on Next button. Select which terminal emulator to use with Git and click on Next button. Configure extr...