Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
JSP Paging Example in Datagrid 
 

In this section, we have developed a web application to create paging in JSP.

 

JSP Paging Example in Datagrid

                         

In this section, we have developed a web application to create paging in JSP. Two files are used "paging.jsp" and "Employee.java" in the code given below.

Brief description of the flow of application :

1). Create a web page "paging.jsp" to display the record.

2). Include the commons-math-1.0-dev.jar, taglibs-datagrid.jar, mysql-connector-java-3.1.6-bin.jar and jstl.jar  in the lib directory.

3).To create a Java Bean file "Employee.java" in the package "emp".

4).  Import all the packages on the "paging.jsp".
<%@ page import="java.util.*"%>
<%@ page import="org.apache.taglibs.datagrid.DataGridParameters"%> 
<%@ page import="java.sql.*"%>
<%@ page import="emp.*"%>
<%@ taglib uri="http://jakarta.apache.org/taglibs/datagrid-1.0" prefix="ui" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>

5). Create arraylist "empList"  to add the Employee object  record.
      ArrayList empList = new ArrayList ();

6). Using  the Taglib to create paging and show records.

Step:1 To create a "register" table in Database 

create table `register` (
`username` varchar (100),
`password` varchar (100),
`firstname` varchar (100),
`lastname` varchar (100),
`email` varchar (100),
`country` varchar (100)
); 

Step:2 To create a web page  "paging.jsp"  

<%@ page contentType="text/html"%>
<%@ page import="java.util.*"%>
<%@ page import="org.apache.taglibs.datagrid.DataGridParameters"%> 
<%@ page import="java.sql.*"%>
<%@ page import="emp.*"%>
<%@ taglib uri="http://jakarta.apache.org/taglibs/datagrid-1.0" prefix="ui" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>

<html>
<head>
<title>jsp paging example in datagrid</title>
<style>
th a:link { text-decoration: none; color: black }
th a:visited { text-decoration: none; color: black }
.rows { background-color: white }
.hiliterows { background-color: pink; color: #000000; font-weight: bold }
.alternaterows { background-color: #efefef }
.header { background-color: cyan; color: #000000;font-weight: bold }

.datagrid { border: 1px solid #C7C5B2; font-family: arial; font-size: 9pt;
font-weight: normal }
</style>
</head>
<body>
<br><br>

<span align="center" style="padding-left:250px;font-size:20px;">Employee Records</span>
<br>
<% ArrayList empList = new ArrayList ();
Employee empObj;
int fromIndex, toIndex;
int tcount =0;
int perpage=4;
int tpage=0;
try {
String connectionURL = "jdbc:mysql://localhost:3306/user_register";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
statement = connection.createStatement();
String QueryString = "SELECT * from register";
rs = statement.executeQuery(QueryString);
while (rs.next()) { 
tcount++;
empObj = new Employee ();
empObj.setFirstName(rs.getString("firstname"));
empObj.setLastName(rs.getString("lastname"));
empObj.setUsername(rs.getString("username"));
empObj.setEmail(rs.getString("email"));
empObj.setCountry(rs.getString("country"));
empList.add(empObj);
}

rs.close();
statement.close();
connection.close();

catch (Exception ex) {
System.out.println("Unable to connect to batabase."+ex);
}
fromIndex = (int) DataGridParameters.getDataGridPageIndex (request, "datagrid1");
if ((toIndex = fromIndex+4) >= empList.size ())
toIndex = empList.size();
request.setAttribute ("empList", empList.subList(fromIndex, toIndex));


pageContext.setAttribute("tCount", tcount);
%>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<ui:dataGrid items="${empList}" var="employee" name="datagrid1" cellPadding="0" 
cellSpacing="0" styleClass="datagrid" >
<columns>

<column width="50">
<header value="" hAlign="left" styleClass="header"/>

</column>

<column width="200">
<header value="Name" hAlign="left" styleClass="header"/>
<item value="${employee.firstName} ${employee.lastName}" hAlign="left" 
styleClass="item"/>


</column>
<column width="200">
<header value="Username" hAlign="left" styleClass="header"/>
<item value="${employee.username}" hAlign="left" styleClass="item"/>


</column>
<column width="200">
<header value="Email" hAlign="left" styleClass="header"/>
<item value="${employee.email}" hAlign="left" styleClass="item"/>

</column>

<column width="100">
<header value="Country" hAlign="left" styleClass="header"/>
<item value="${employee.country}" hAlign="left" styleClass="item"/>

</column>

</columns>
<rows styleClass="rows" hiliteStyleClass="hiliterows"/>
<alternateRows styleClass="alternaterows"/>

<paging size="4" count="${tCount}" custom="true" nextUrlVar="next" 
previousUrlVar="previous" pagesVar="pages"/>
<order imgAsc="up.gif" imgDesc="down.gif"/>
</ui:dataGrid>
<table width="750" style="font-family: arial; font-size: 10pt" border=0>

<tr>
<td align="left" width="33%">
<c:if test="${previous != null}">
<a href="<c:out value="${previous}"/>">Previous</a>
</c:if>&nbsp;
</td>
<td align="center" width="33%">
<c:forEach items="${pages}" var="page">
<c:choose>
<c:when test="${page.current}">
<b><a href="<c:out value="${page.url}"/>"><c:out value="${page.index}"/></a></b>
</c:when>
<c:otherwise>
<a href="<c:out value="${page.url}"/>"><c:out value="${page.index}"/></a>
</c:otherwise>
</c:choose>
</c:forEach>
</td>
<td align="right" width="33%">&nbsp;
<c:if test="${next != null}">
<a href="<c:out value="${next}"/>">Next</a>
</c:if>
</td>
</tr>
</table>
</body>
</html>

 Step:3 To create a "Employee.java" 

package emp;
public class Employee {

      protected String firstName;
      protected String lastName;
      protected String username;
      protected String email;
      protected String country;
      

      public String getFirstName () {
        return (firstName);
      }
        public String getLastName (){
        return (lastName);
      }      
      public String getUsername () {
        return (username);
      }            
      public String getEmail() {
        return (email);
        }
      
      public String getState () {
        return (email);
        }   
      
      public String getCountry () {
        return (country);
        }      

      public void setFirstName (String newFirstName)
             {
        firstName = newFirstName;
        }
        public void setLastName (String newLastName)
        {
        lastName = newLastName;
        }

       public void setUsername (String newUsername)
             {
        username = newUsername;
        }

        public void setEmail (String newEmail)
        {
        email = newEmail;
        } 
        
        public void setCountry(String newCountry)
        {
        country = newCountry;
        } 


      }


Output:

Display records of first page.


Display the Second or Next page.

To navigate first page click on the  "Previous" or "1".

Download the application

                         

» View all related tutorials
Related Tags: c com orm table ant data join select form make tables tab for to between e ul result pe from

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (
post your own) View All Comments Latest 10 Comments:

datagrid api from Jakarta is deprecated can is still use it now.

Posted by siva on Saturday, 04.4.09 @ 16:53pm | #86504

I have two textbox one for ID and Other for Name and when i input ID in first box at that time search record in database if there are present the corresponding Name automatically come in the next Text box............otherwise ask me to create new User id and record.

plz someone help me out...........

Posted by azhar on Wednesday, 10.8.08 @ 00:25am | #80954

org.apache.jasper.JasperException: According to TLD or attribute directive in tag file, attribute items does not accept any expressions

Posted by Sachin on Tuesday, 09.30.08 @ 18:46pm | #80800

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.