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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Migrating From the OpenAI API to vLLM: Configure It Right
  • Constrained Decoding Models Explained: Encoders, Decoders and When to Use Them
  • Building a Reddit Scraper in Go: JSON Listings, Pagination, Rate Limits, and Where It Breaks
  • Exploring the DBMS_DEVELOPER Package: JSON Metadata for Oracle Objects

Trending

  • Engineering Responsible AI for High-Stakes Financial Applications
  • Introducing TypeSafe Jev: Typed Judgments as Programming Primitives
  • AI Didn't Replace Software Engineering; It Changed the Bottleneck
  • Getting Started With Weaviate: Building AI-Powered Search Applications With Java

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Aug. 27, 13 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
90.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Migrating From the OpenAI API to vLLM: Configure It Right
  • Constrained Decoding Models Explained: Encoders, Decoders and When to Use Them
  • Building a Reddit Scraper in Go: JSON Listings, Pagination, Rate Limits, and Where It Breaks
  • Exploring the DBMS_DEVELOPER Package: JSON Metadata for Oracle Objects

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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