DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Give Your AI Assistant Long-Term Memory With perag
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives

Trending

  • The 7 Pillars of Meeting Design: Transforming Expensive Conversations into Decision Assets
  • The Missing `bandit` for AI Agents: How I Built a Static Analyzer for Prompt Injection
  • 8 RAG Patterns You Should Stop Ignoring
  • A Deep Dive into Tracing Agentic Workflows (Part 2)
  1. DZone
  2. Coding
  3. Frameworks
  4. How to create a JQuery DataTable using JSON and Servlet

How to create a JQuery DataTable using JSON and Servlet

By 
Nitin Kumar user avatar
Nitin Kumar
·
Aug. 27, 13 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
90.2K Views

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

jquery datatabl

conclusion
you can download full source code from github link and most welcome to fork or update the same.

references:

http://datatables.net/examples/

JQuery JSON

Published at DZone with permission of Nitin Kumar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Give Your AI Assistant Long-Term Memory With perag
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook