Share on Google+Share on Google+

JSON and Servlet example

In the previous section of JSON-Java example you have learned how to create a java class by using JSON classes.

JSON and Servlet example


In the previous section of JSON-Java example you have learned how to create a java class by using JSON classes. Now in this example we will tell you how to use JSON classes for creating Servlet.

In this example we have created  an object of JSONArray and then we have added elements into this array by using the method add(). To have functionality of JSON in your application you must have JSON-lib and its supported jar files. These are: commons-lang.jar, commons-beanutils.jar, commons-collections.jar, commons-logging.jar, ezmorph.jar and json-lib-2.2.2-jdk15.jar

Here is the example code of as follows:  

import javax.servlet.*;
import javax.servlet.http.*;
import net.sf.json.JSONArray;

public class JSONServlet extends  HttpServlet{
  public void doGet(HttpServletRequest request,
  HttpServletResponse response
   throws ServletException,IOException{
 JSONArray arrayObj=new JSONArray();
 arrayObj.add("Amit Kumar");
 arrayObj.add(new Double(66.67));
  PrintWriter out = response.getWriter();
  for(int i=0;i<arrayObj.size();i++){

We have to do corresponding servlet mapping into web.xml as given below:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns=""

To run this example follow this step by step procedure:

  • create a class
  • compile JSONServlet and place it into WEB-INF/classes directory
  • Download JSONLibraries and place it into Tomcat's lib directory
  • Start Tomcat Webserver and type
    http://localhost:8080/JSON/JSONServlet  into browser's address bar you will have following output on your browser.


Download JSON-Servlet Project code




Posted on: September 27, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: JSON and Servlet example   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 24, 2011

how to get json object from the other system using servlets.
Sérgio Carvalho
August 18, 2011
Servlet with JSON

I know it's been a while since you wrote this, bue today it gave me a great help So thank you :)
June 18, 2012
java and servlet

I hv used servlet with JSON but i am getting an error package net.sf.json.JSONObject does not exit.......!! 1) I had download Json 6 jar files 2)then i have placed them in C:\Program Files\Java\jre1.6.0\JSON (JSON is a folder thar contain those 6 jar files) 3)then i have right clicked My Computer click the properties.... then Advance->Enviroment Variables-> and then i have set the path C:\Program Files\Java\jre1.6.0\JSON finally click okk.. 4) Then i hv runned my program in command prompt using this line--- D:\>javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 5.5\com mon\lib\servlet-api.jar" but it is not 6 error of cannot find symbol getWriter(),canot find class JSONArray..... can u please look into the matter..and guide me wt to do..i m badly strucked...!!!!