This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How do I send json data from a server?

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am not cent percent sure if a lot of the wordings used in this post is going to be correct since I'm new to json and the web.
My purpose is to use json data from a server and parse it and show meaningful data to the user. I am aware of json parsing, so no sweat there. However, I would like to know how json data is sent to the front end after which it can be parsed.

Usually in my json parsing examples, I have a variable like var data = ]OR I do a getJSON on a data.json file which I have stored in the project folder. However, I want this data to come real time from a servlet. Can you guide me on how to go about this?

So, basically my question is, how do I send real time json data from a servlet backend, say some records which I have extracted from the database using jdbc?

Thanks,
 
Rancher
Posts: 43011
76
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first advice would be not to use a servlet to send the JSON, but a REST web service using a library like Jersey. That's really how you implement WS these days, and it makes it real easy to create JSON from Java objects. Start reading here: https://jersey.java.net/
 
Marshal
Posts: 67275
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also look into using a JSON library such as Gson or Jackson. You would return the JSON text as the response body, and set the content type header appropriately.
 
Greenhorn
Posts: 3
MySQL Database C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simply write this in your servlet:




and then send the JSON data as follows:


data is the JSON data your want to send and it should fit the JSON data format.

Hope this helps!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!