C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

Page count implementation using JSP (Java Server Page)

By: Vanka Manikanth, on 09 MAR 2017

Learn: How to write code for page count in JSP (Java Server Page) with JDBC?

We observe the PAGE COUNT for many websites, mostly all of they get the readymade code from other resources. But frankly building your own logic for the PAGE COUNT is better, because getting the third party code they can push their promotion URL, may have a chance to penetrate the useless scripting to your website. So in order to get rid of all those. Here we IncludeHelp was implemented a PAGE COUNT logic.

The below consist of page count using JSP with JDBC connection.

Steps before you move to JSP

  1. Create a Database table in your oracle/MYSQL etc.
  2. Create a column as Counter.
  3. Initially place zero (0) value.
  4. Create a JSP file, read the value from the Database.
  5. Increment the value which you got.
  6. Update the value in the database.
  7. Display the updated PageHit value.
  8. Done!

PAGECOUNT.jsp

Here firstly we read the value from the database and set the value to application level attribute and we incremented the value, soon as we increment the table gets updated with the incremented value and displays the incremented value.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.io.*,java.sql.*, java.util.*"%>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>PAGE COUNT</title>
		<style>
			.pageHits
			{
				width:500px;
				padding:21px;
				border:3px solid #FFF;
				font-size: 24px;
				text-align: center;
				color:#FFF;
				background-color: #216;
			}
		</style>
	</head>
	<body>
	<%int status=0;
	try{
		Class.forName("com.mysql.jdbc.Driver");
		Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/MYDB","root","new_password");	
		PreparedStatement ps =con.prepareStatement("SELECT COUNTER FROM HITSCOUNTER");
		ResultSet rs =ps.executeQuery();
		while(rs.next()){
		status =rs.getInt(1);
		}
		
	}catch(Exception e){
		System.out.println(e);
	}
	 Integer Hits =(Integer)application.getAttribute("HitCounter");
	 if( (Hits ==0) || (Hits ==null) ){
	 
	 Hits = status;
	 Hits++;
	 }else{%>
			
	 <% 
	 
	 Hits=status;
	 status =Hits++;
	 }
	 application.setAttribute("HitCounter", status);

	%>
	<%try {
		Class.forName("com.mysql.jdbc.Driver");
		Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/MYDB","root","new_password");	
		PreparedStatement ps = con.prepareStatement("UPDATE `hitscounter` SET `Counter`="+Hits+";");
		status = ps.executeUpdate();
		if(status!=0){%>
		<div class="pageHits">PAGE HITS <%= Hits%></div>
		<%}
		%>

		<%con.close();
	}catch (Exception e) {
		System.out.println(e);		
	}

	%>
	</body>
</html>





Output:

code for page count in JSP (Java Server Page) with JDBC

Note: You can check this by turning off your server and get started it back. The recent value in the database exists and gets incremented in accordance. So to start the counter from initial state you have to make the database counter value to ZERO and then close the window and re-run the JSP. That's it.


COMMENTS