Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I send json data from a server?  RSS feed

 
Aftab Hassan
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,
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • 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/
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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.
 
Nivedita Gautam
Greenhorn
Posts: 3
C++ Java MySQL Database
  • 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!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!