Gridview in Servlets using jQuery DataTables plugin
In this article we shall learn the basic coding that required to create JQuery DataTable using JSON passed by servlet. DataTable is a jQuery plugin which adds a lot of functionality to plain HTML tables, such as filtering, paging sorting, changing page length, server side processing etc.
Library required
Installation
Above download will provide two JQuery plugin jquery.js and jquery.dataTables.js
<script src="js/jquery.js"></script> <script src="js/jquery.dataTables.js"></script>
Default stylesheet which shipped with latest DataTable download package
<link href="css/demo_table_jui.css" rel="stylesheet" /> <link href="css/jquery-ui.css" rel="stylesheet" /> <link href="css/demo_page.css" rel="stylesheet" />
Project Structure
Create a dynamic web project with following folder structure and place the above files as shown.
Model class
package com.model; public class RevenueReport { public RevenueReport(String company, String country, int year, int revenue) { this.company = company; this.country = country; this.year = year; this.revenue = revenue; } private String company; private String country; private int year; private int revenue; public String getCountry() { return country; } public int getRevenue() { return revenue; } public String getCompany() { return company; } public int getYear() { return year; } public void setCountry(String country) { this.country = country; } public void setRevenue(int revenue) { this.revenue = revenue; } public void setCompany(String company) { this.company = company; } public void setYear(int year) { this.year = year; } }
Recommended reading:
Business class
This Business Service class which provides static data using model class.
package com.service; import java.util.LinkedList; import java.util.List; import com.model.RevenueReport; public class BusinessService { public static List<RevenueReport> getCompanyList() { List<RevenueReport> listOfCompany = new LinkedList<RevenueReport>(); listOfCompany.add(new RevenueReport("Bosch", "Germany",2012, 40000)); listOfCompany.add(new RevenueReport("XYZ", "India",2014, 10000)); listOfCompany.add(new RevenueReport("Robotics", "United Kingdom",2011, 35000)); listOfCompany.add(new RevenueReport("Merch", "USA",2010, 20000)); listOfCompany.add(new RevenueReport("Foxtron", "Indonesia",2009, 15000)); listOfCompany.add(new RevenueReport("Benz", "Germany",2013, 50000)); listOfCompany.add(new RevenueReport("Audi", "United Kingdom",2012, 60000)); listOfCompany.add(new RevenueReport("Hyat", "France",2011, 30000)); listOfCompany.add(new RevenueReport("HCL", "India",2007, 23000)); listOfCompany.add(new RevenueReport("CTS", "USA",2010, 42000)); listOfCompany.add(new RevenueReport("Accenture", "France",2008, 15000)); listOfCompany.add(new RevenueReport("Air India", "India",2005, 10000)); listOfCompany.add(new RevenueReport("Kingfisher", "India",2011, 8000)); listOfCompany.add(new RevenueReport("Vodaphone", "Netharland",2006, 45000)); return listOfCompany; } }
Jsp
<!DOCTYPE html> <html> <head> <title>Gridview in Servlet using jQuery DataTable plugin</title> <link href="css/demo_table_jui.css" rel="stylesheet" /> <link href="css/jquery-ui.css" rel="stylesheet" /> <link href="css/demo_page.css" rel="stylesheet" /> <script src="js/jquery.js"></script> <script src="js/jquery.dataTables.js"></script> <script> // Ajax call to Servlet to display data via DataTables $(document).ready(function() { $(".jqueryDataTable").dataTable({ "sPaginationType" : "full_numbers", "bProcessing" : false, "bServerSide" : false, "sAjaxSource" : "displayData", "bJQueryUI" : true, "aoColumns" : [ { "mData": "company" }, { "mData": "country" }, { "mData": "year" }, { "mData": "revenue" } ] } ); } ); </script> </head> <body id="dt_example"> <div id="container"> <h1>Ajax based Gridview using jQuery DataTable plugin</h1> <div id="demo_jui"> <table class="display jqueryDataTable"> <thead> <tr> <th>Company</th> <th>Country</th> <th>Year</th> <th>Revenue</th> </tr> </thead> <tbody> </tbody> </table> </div> </div> </body> </html>
DataTable Parameters class
In reply to each request for information that DataTables makes to the server, it expects to get a well formed JSON object with some parameters.So Create a DataTable Parameters class with all those required parameters
package com.dataTable; import java.util.List; import com.model.RevenueReport; public class DataTableParameters { // Data table plugin parameter int iTotalRecords; int iTotalDisplayRecords; String sEcho; String sColumns; List<RevenueReport> aaData; public int getiTotalRecords() { return iTotalRecords; } public void setiTotalRecords(int iTotalRecords) { this.iTotalRecords = iTotalRecords; } public int getiTotalDisplayRecords() { return iTotalDisplayRecords; } public void setiTotalDisplayRecords(int iTotalDisplayRecords) { this.iTotalDisplayRecords = iTotalDisplayRecords; } public String getsEcho() { return sEcho; } public void setsEcho(String sEcho) { this.sEcho = sEcho; } public String getsColumns() { return sColumns; } public void setsColumns(String sColumns) { this.sColumns = sColumns; } public List<RevenueReport> getAaData() { return aaData; } public void setAaData(List<RevenueReport> aaData) { this.aaData = aaData; } }
Servlet implementation
package com.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.dataTable.DataTableParameters; import com.model.RevenueReport; import com.service.BusinessService; public class DataTableServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); // Call business service class to get list of company List<RevenueReport> listOfCompany = BusinessService.getCompanyList(); DataTableParameters dataTableParam = new DataTableParameters(); // Set the list fetched in aaData dataTableParam.setAaData(listOfCompany); Gson gson = new GsonBuilder().setPrettyPrinting().create(); // Convert Java Object to Json String json = gson.toJson(dataTableParam); response.getWriter().print(json); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
web.xml
Make sure you have done servlet mapping properly in web.xml file. An example of this is given below,
<web-app> <servlet> <display-name>displayData</display-name> <servlet-name>displayData</servlet-name> <servlet-class>com.servlet.DataTableServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>displayData</servlet-name> <url-pattern>/displayData</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
Reference
jQuery DataTables in Java Web Applications