Skip to main content

How to use JSON(JavaScript Object Notation) in NetBeans


JSON: JavaScript Object Notation.

JSON is syntax for storing and exchanging text information. Much like XML.

JSON is smaller than XML, and faster and easier to parse.
First Create simple jsp page in netbeans:
index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>AJAX calls to Servlet using JQuery and JSON</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
   1:  
   2: <script>
   3:     $(document).ready(function() {
   4:         $('#country').change(function(event) {
   5:         var $country=$("select#country").val();
   6:            $.get('ActionServlet',{countryname:$country},function(responseJson) {
   7:             var $select = $('#states');
   8:                $select.find('option').remove();
   9:                $.each(responseJson, function(key, value) {
  10:                    $('<option>').val(key).text(value).appendTo($select);
  11:                     });
  12:             });
  13:         });
  14:     });
</script> </head> <body> <h1>AJAX calls to Servlet using JQuery and JSON</h1> Select Country: <select id="country"> <option>Select Country</option> <option value="India">India</option> <option value="US">US</option> </select> <br/> <br/> Enter: <select id="states"> <option>Select State</option> </select> </body> </html>

ActionServlet.java

package ajaxdemo;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ActionServlet extends HttpServlet {
   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
   String country=request.getParameter("countryname");
  Map<String, String> ind = new LinkedHashMap<String, String>();
     ind.put("1", "New delhi");
     ind.put("2", "Tamil Nadu");
     ind.put("3", "Kerala");
     ind.put("4", "Andhra Pradesh");

     Map<String, String> us = new LinkedHashMap<String, String>();
     us.put("1", "Washington");
     us.put("2", "California");
     us.put("3", "Florida");
     us.put("4", "New York");
     String json = null ;
     if(country.equals("India")){
      json= new Gson().toJson(ind);
     }
     else if(country.equals("US")){
      json=new Gson().toJson(us);
     }
     response.setContentType("application/json");
     response.setCharacterEncoding("UTF-8");
     response.getWriter().write(json);

        } finally { 
            out.close();
        }
    } 

Download JSON Jar file.

Output:

1


2


3


4


Technorati Tags:

Comments

Popular posts from this blog

WAP to calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for upto 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any call beyond 200 calls.

  #include<iostream.h> #include<conio.h> void main() { int calls; float bill; cout<<" Enter number of calls : "; cin>>calls; if (calls<=100) bill=200; else if (calls>100 && calls<=150) { calls=calls-100; bill=200+(0.60*calls); } else if (calls>150 && calls<=200) { calls=calls-150; bill=200+(0.60*50)+(0.50*calls); } else { calls=calls-200; bill=200+(0.60*50)+(0.50*50)+(0.40*calls); } cout<<" Your bill is Rs. "<<bill; getch(); }   Output: Enter number of calls : 190 Your bill is Rs.250

Write a program to calculate the total expenses. Quantity and price per item are input by the user and discount of 10% is offered if the expense is more than 7000.

  #include<iostream.h> #include<conio.h> void main() { int totalexp, qty, price, discount; cout<<" Enter quantity: "; cin>>qty; cout<<" Enter price: "; cin>>price; totalexp=qty*price; if (totalexp>7000) { discount=(totalexp*0.1); totalexp=totalexp-discount; } cout<<" Total Expense is Rs. "<<totalexp; getch(); }