Overview
You can use this with any programming language. You can use this with .jsp, .php etc. Here we have given one simple web application using Servlet and JSP.
Pre-Requisites:
- Browser which supports JavaScript
Pre-Requisites for running the below program:
- Browser which supports JavaScript
- jdk1.6.0_11 ( works with previous versions of JDK)
- Note: for previous versions of JDK. All the .java files should be compiled once again. Make sure that all the supporing files are in classpath. To explore more about classpath refer Set Classpath
- apache-tomcat-6.0.16 (Works with previous versions)
Sample program
Save as index.jsp
Path (\webapps\index.jsp)
Path (\webapps\index.jsp)
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!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>Pagination Using JavaScript Example -
- www.javaworkspace.com</title>
- </head>
- <body>
- <h1>Pagination Using JavaScript Example - <a
- href="http://www.javaworkspace.com" target="_blank">www.javaworkspace.com</a></h1>
- <form method="post" action="Controller"><input type="submit"
- value="Get Report"></form>
- </body>
- </html>
Save as Controller.java
Path (\webapps\PaginationUsingJavaScript\WEB-INF\classes\com\javaworkspace\pagination\controller\Controller.java)
Path (\webapps\PaginationUsingJavaScript\WEB-INF\classes\com\javaworkspace\pagination\controller\Controller.java)
- /*
- * Controller.java
- */
- package com.javaworkspace.pagination.controller;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import com.javaworkspace.pagination.dto.StudentDetailsDTO;
- /**
- * @author www.javaworkspace.com
- *
- */
- public class Controller extends HttpServlet {
- static final long serialVersionUID = 1L;
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- List list = new ArrayList();
- HttpSession httpSession = request.getSession();
- /*
- * Hard-coded sample data. Normally this would come from a real data
- * source such as a database
- */
- list.add(new StudentDetailsDTO("Mark", "John", 25, "India"));
- list.add(new StudentDetailsDTO("Harry", "Scott", 35, "England"));
- list.add(new StudentDetailsDTO("Nathan", "Ridley", 12, "America"));
- list.add(new StudentDetailsDTO("Tom", "Hanks", 55, "France"));
- list.add(new StudentDetailsDTO("Roger", "Chris", 65, "Germany"));
- list.add(new StudentDetailsDTO("Antony", "Jason", 45, "Denmark"));
- httpSession.setAttribute("studentDetails", list);
- // RequestDispatcher dispatcher = request
- // .getRequestDispatcher("reportWithoutPagination.jsp");
- RequestDispatcher dispatcher = request
- .getRequestDispatcher("paginationUsingJavaScript.jsp");
- dispatcher.forward(request, response);
- }
- }
Save as StudentDetailsDTO.java
Path (\webapps\PaginationUsingJavaScript\WEB-INF\classes\com\javaworkspace\pagination\dto\StudentDetailsDTO.java)
Path (\webapps\PaginationUsingJavaScript\WEB-INF\classes\com\javaworkspace\pagination\dto\StudentDetailsDTO.java)
- /**
- * StudentDetailsDTO.java
- */
- package com.javaworkspace.pagination.dto;
- import java.io.Serializable;
- /**
- * @author www.javaworkspace.com
- *
- */
- public class StudentDetailsDTO implements Serializable {
- private String studentName;
- private String fatherName;
- private int age;
- private String country;
- public StudentDetailsDTO(String studentName, String fatherName, int age,
- String country) {
- this.studentName = studentName;
- this.fatherName = fatherName;
- this.age = age;
- this.country = country;
- }
- public String getStudentName() {
- return studentName;
- }
- public void setStudentName(String studentName) {
- this.studentName = studentName;
- }
- public String getFatherName() {
- return fatherName;
- }
- public void setFatherName(String fatherName) {
- this.fatherName = fatherName;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getCountry() {
- return country;
- }
- public void setCountry(String country) {
- this.country = country;
- }
- }
Save as paginationUsingJavaScript.jsp
Path (\webapps\PaginationUsingJavaScript\paginationUsingJavaScript.jsp)
Path (\webapps\PaginationUsingJavaScript\paginationUsingJavaScript.jsp)
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <%@page import="java.util.List"%>
- <%@page import="com.javaworkspace.pagination.dto.StudentDetailsDTO"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <style type="text/css">
- .pg-normal {
- color: #0000FF;
- font-weight: normal;
- text-decoration: none;
- cursor: pointer;
- }
- .pg-selected {
- color: #800080;
- font-weight: bold;
- text-decoration: underline;
- cursor: pointer;
- }
- </style>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Pagination Using Java Script Tutorial -
- www.javaworkspace.com</title>
- </head>
- <script type="text/javascript">
- function Pager(tableName, itemsPerPage) {
- this.tableName = tableName;
- this.itemsPerPage = itemsPerPage;
- this.currentPage = 1;
- this.pages = 0;
- this.inited = false;
- this.showRecords = function(from, to) {
- var rows = document.getElementById(tableName).rows;
- // i starts from 1 to skip table header row
- for (var i = 1; i < rows.length; i++) {
- if (i < from || i > to)
- rows[i].style.display = 'none';
- else
- rows[i].style.display = '';
- }
- }
- this.showPage = function(pageNumber) {
- if (! this.inited) {
- alert("not inited");
- return;
- }
- var oldPageAnchor = document.getElementById('pg'+this.currentPage);
- oldPageAnchor.className = 'pg-normal';
- this.currentPage = pageNumber;
- var newPageAnchor = document.getElementById('pg'+this.currentPage);
- newPageAnchor.className = 'pg-selected';
- var from = (pageNumber - 1) * itemsPerPage + 1;
- var to = from + itemsPerPage - 1;
- this.showRecords(from, to);
- }
- this.prev = function() {
- if (this.currentPage > 1)
- this.showPage(this.currentPage - 1);
- }
- this.next = function() {
- if (this.currentPage < this.pages) {
- this.showPage(this.currentPage + 1);
- }
- }
- this.init = function() {
- var rows = document.getElementById(tableName).rows;
- var records = (rows.length - 1);
- this.pages = Math.ceil(records / itemsPerPage);
- this.inited = true;
- }
- this.showPageNav = function(pagerName, positionId) {
- if (! this.inited) {
- alert("not inited");
- return;
- }
- var element = document.getElementById(positionId);
- var pagerHtml = '<span onclick="' + pagerName + '.prev();" class="pg-normal"> « Prev </span> | ';
- for (var page = 1; page <= this.pages; page++)
- pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span> | ';
- pagerHtml += '<span onclick="'+pagerName+'.next();" class="pg-normal"> Next »</span>';
- element.innerHTML = pagerHtml;
- }
- }
- </script>
- <body>
- <h1>Pagination Using Java Script</h1>
- <%
- List list = (List) session.getAttribute("studentDetails");
- %>
- <table border="1" id="results">
- <tr bgcolor="orange">
- <td><strong>Student Name</strong></td>
- <td><strong>Father Name</strong></td>
- <td><strong>Age</strong></td>
- <td><strong>Country</strong></td>
- </tr>
- <%
- for (int i = 0; i < list.size(); i++) {
- %>
- <tr>
- <%
- StudentDetailsDTO studentDetailsDTO = (StudentDetailsDTO) list
- .get(i);
- out.println("<td>" + studentDetailsDTO.getStudentName()
- + "</td>");
- out.println("<td>" + studentDetailsDTO.getFatherName()
- + "</td>");
- out.println("<td>" + studentDetailsDTO.getAge() + "</td>");
- out.println("<td>" + studentDetailsDTO.getCountry() + "</td>");
- %>
- </tr>
- <%
- }
- %>
- </table>
- <div id="pageNavPosition"></div>
- <script type="text/javascript"><!--
- var pager = new Pager('results', 2);
- pager.init();
- pager.showPageNav('pager', 'pageNavPosition');
- pager.showPage(1);
- //--></script>
- </body>
- </html>
Save as web.xml
Path (\WEB-INF\web.xml)
Path (\WEB-INF\web.xml)
- <!--?xml version="1.0" encoding="UTF-8"?-->
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
- <display-name>Pagination Using JavaScript</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- <servlet>
- <description>
- Display Tag Example www.javaworkspace.com
- </description>
- <display-name>Controller</display-name>
- <servlet-name>Controller</servlet-name>
- <servlet-class>
- com.javaworkspace.pagination.controller.Controller
- </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>Controller</servlet-name>
- <url-pattern>/Controller</url-pattern>
- </servlet-mapping>
- </web-app>
- In Controller.java uncomment any one to get the report accordingly.
RequestDispatcher dispatcher = request
.getRequestDispatcher("paginationUsingJavaScript"); //RequestDispatcher dispatcher = request
//.getRequestDispatcher("reportWithoutPagination.jsp");
.getRequestDispatcher("paginationUsingJavaScript"); //RequestDispatcher dispatcher = request
//.getRequestDispatcher("reportWithoutPagination.jsp");
- var pager = new Pager('results', 2); - Here 2 is the number of records per page. You can change the number of records per page using this.
- Edit the stylesheet to change the link color ... etc.
- You can even place the styles sheet and javascript externally.
If you are not a Java Programmer. You can follow the below program. Below example is just a .html program. Here in the below example we have hard coded all the values. You can generate those value dynamically using php,asp...etc
- <html version="-//W3C//DTD HTML 4.01 Transitional//EN">
- <head>
- <title>Pagination Using JavaScript - www.javaworkspace.com</title>
- <style type="text/css">
- .pg-normal {
- color: #0000FF;
- font-weight: normal;
- text-decoration: none;
- cursor: pointer;
- }
- .pg-selected {
- color: #800080;
- font-weight: bold;
- text-decoration: underline;
- cursor: pointer;
- }
- </style>
- </head>
- <script type="text/javascript">
- function Pager(tableName, itemsPerPage) {
- this.tableName = tableName;
- this.itemsPerPage = itemsPerPage;
- this.currentPage = 1;
- this.pages = 0;
- this.inited = false;
- this.showRecords = function(from, to) {
- var rows = document.getElementById(tableName).rows;
- // i starts from 1 to skip table header row
- for (var i = 1; i < rows.length; i++) {
- if (i < from || i > to)
- rows[i].style.display = 'none';
- else
- rows[i].style.display = '';
- }
- }
- this.showPage = function(pageNumber) {
- if (! this.inited) {
- alert("not inited");
- return;
- }
- var oldPageAnchor = document.getElementById('pg'+this.currentPage);
- oldPageAnchor.className = 'pg-normal';
- this.currentPage = pageNumber;
- var newPageAnchor = document.getElementById('pg'+this.currentPage);
- newPageAnchor.className = 'pg-selected';
- var from = (pageNumber - 1) * itemsPerPage + 1;
- var to = from + itemsPerPage - 1;
- this.showRecords(from, to);
- }
- this.prev = function() {
- if (this.currentPage > 1)
- this.showPage(this.currentPage - 1);
- }
- this.next = function() {
- if (this.currentPage < this.pages) {
- this.showPage(this.currentPage + 1);
- }
- }
- this.init = function() {
- var rows = document.getElementById(tableName).rows;
- var records = (rows.length - 1);
- this.pages = Math.ceil(records / itemsPerPage);
- this.inited = true;
- }
- this.showPageNav = function(pagerName, positionId) {
- if (! this.inited) {
- alert("not inited");
- return;
- }
- var element = document.getElementById(positionId);
- var pagerHtml = '<span onclick="' + pagerName + '.prev();" class="pg-normal"> « Prev </span> | ';
- for (var page = 1; page <= this.pages; page++)
- pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span> | ';
- pagerHtml += '<span onclick="'+pagerName+'.next();" class="pg-normal"> Next »</span>';
- element.innerHTML = pagerHtml;
- }
- }
- </script>
- <body>
- <form action="" method="get" enctype="application/x-www-form-urlencoded">
- <h1>Pagination Using JavaScript - <a
- href="http://www.javaworkspace.com" target="_blank">www.javaworkspace.com</a></h1>
- <table id="results">
- <tr>
- <th>#</th>
- <th>field</th>
- </tr>
- <tr>
- <td>1</td>
- <td>rajeev</td>
- </tr>
- <tr>
- <td>2</td>
- <td>ramesh</td>
- </tr>
- <tr>
- <td>3</td>
- <td>Rahul</td>
- </tr>
- <tr>
- <td>4</td>
- <td>Bala</td>
- </tr>
- <tr>
- <td>5</td>
- <td>Nathan</td>
- </tr>
- <tr>
- <td>6</td>
- <td>Robin</td>
- </tr>
- <tr>
- <td>7</td>
- <td>Sambha</td>
- </tr>
- <tr>
- <td>8</td>
- <td>Arjun</td>
- </tr>
- <tr>
- <td>9</td>
- <td>Satyan</td>
- </tr>
- <tr>
- <td>10</td>
- <td>Singapore</td>
- </tr>
- </table>
- <br />
- <div id="pageNavPosition"></div>
- <br />
- <div><input type="submit"
- onclick="alert('Hey, this is just a sample!'); return false;" /> <input
- type="reset" /></div>
- </form>
- <script type="text/javascript"><!--
- var pager = new Pager('results', 3);
- pager.init();
- pager.showPageNav('pager', 'pageNavPosition');
- pager.showPage(1);
- //--></script>
- </body>
- </html>
0 comments:
Post a Comment