How to create a JQuery DataTable using JSON and Servlet
Join the DZone community and get the full member experience.
Join For Free
in this article i’ll introduce the basic coding that require to create jquery datatable using json passed by simple servlet.
datatable is very powerful jquery based grid with advance features which can be build in short span of time with customize features.
installation
1. download latest
jquery datatable download
2. above download will provide two jquery plugin jquery.js and querytables.js
<script type="text/javascript" charset="utf-8" src="/datatables/media/js/jquery.js"></script> <script type="text/javascript" charset="utf-8" src="/datatables/media/js/jquery.datatables.js"></script>
3. default stylesheet which shipped with latest datatable download package
<style type="text/css" title="currentstyle"> @import "../resources/css/demo_table.css"; </style>
note: you can download full source code from github link
creating the datatable
we can write below code to create the basic datatable with data
feedsummary.jsp
==========================
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { var otable = $('#tableid').datatable( { "processing": true, "ajax": { "url": "/feedsummaryupdate/feedservlet", "datasrc": "demo", "type": "get" } } ); } ); </script>
$(document).ready will ready to execute the javascript and var otable = $(‘#tableid’).datatable says that write datatable on tableid place.
datatables will adding sorting, filtering, paging and information to your table by default, providing the end user of your web-site with the ability to control the display of the table and find the information that they want from it as quickly as possible.
the pointer tableid and column name will be defined in table tag as below
feedsummary.jsp
=====================
<table cellpadding="0" cellspacing="0" border="0" id="tableid"> <thead> <tr> <th width="10%">first name</th> <th width="10%">last name</th> <th width="10%">address 1</th> <th width="10%">address 2</th> </tr> </thead> </table>
above datatable code invoke feedservlet which will return json string as defined below
feedservlet.java
===============
protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { printwriter out = response.getwriter(); string json = "{ \"demo\":[[\"first name\",\"last name\","+ +\"address1\",\"address2\"],[\"first name\",\"last name\",\"address1\",\"address2\"]]}"; out.println(json); }
now either we can use servlet annotation or web.xml as below to register above feedservlet
web.xml
=========
<servlet> <description></description> <display-name>feedservlet</display-name> <servlet-name>feedservlet</servlet-name> <servlet-class>feedservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>feedservlet</servlet-name> <url-pattern>/feedservlet</url-pattern> </servlet-mapping>
running
incorporate the above point and deploy with server to view the result as follows:
http://localhost:8080/exampledatatablejson/feedsummary.jsp
jquery datatable image
conclusion
you can download full source code from
github link
and most welcome to fork or update the same.
references:
Published at DZone with permission of Nitin Kumar, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments