JSON with Jquery JSP Servlets

A simple JSON example with jquery JSP Servlets...

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>Insert title here</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){ 
            $('#getData').click(function(){
                $.ajax({
                    url:'JsonServlet',
                    type:'post',
                    dataType: 'json',
                    success: function(data) {
                        $('#name').val(data.name);
                        $('#email').val(data.email);
                    }
                });
            });
    });
</script>
</head>
<body>

    Name:<input type="text" id="name"/><br/>
    Email:<input type="text" id="email"/>
    
    <input type="button" id="getData" value="Get Data"/> 

</body>
</html>

JsonServlet.java
package codesstore.json;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

public class JsonServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public JsonServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        PrintWriter out= response.getWriter();
        JSONObject json = new JSONObject();
        json.put("name", "Sameera Jayasekara");
        json.put("email", "codesstore@blogspot.com");
        out.print(json);
    
    }
    

}

Used json-rpc-1.0.jar file

29 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I am getting
    throw Components.Exception("prompt aborted by user", Cr.NS_ERROR_NOT_AVAILABLE);
    above exception

    ReplyDelete
  4. hi

    this is very good example , this is help me alot to understand json.
    but i need one example on page load dropdown box example with servlets and jsp or spring mvc

    i am trying my self not working .

    ReplyDelete
  5. Hi.......Here ur code will get the data from Servlet and showing in JSP............How to send Data from UI inputs in JSP to servlets using JSON...............and how to recive the same data in Servlet sent from JSP .............Kindly assist.............

    ReplyDelete
    Replies
    1. Sending data is easy.

      javascript code

      $('#postData').click(function(){

      var name = $("#name").val();
      var email = $("#email").val();
      $.ajax({
      url:'JsonPostServlet',
      type:'post',
      data:{name:name,email:email},
      dataType: 'json',
      success: function(data) {

      }
      });
      });

      HTML code

      Name:<input type="text" value="sam" id="name"/>
      Email:<input type="text" value="sam@codesstore.blobspot.com" id="email"/>
      <input type="button" id="postData" value="Post Data"/>

      JsonPostServlet code

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String name = request.getParameter("name");
      String email = request.getParameter("email");
      System.out.println(name+" "+email);
      }

      Delete
  6. thank u for this nice example...

    ReplyDelete
  7. hi i m using HandleBars Framework in that i am getting data from database using servlet and i am converting the list(data) value into Json values later i need to pass the values to JSP and View the values in tat Page.. please tell me some ideas and how to work it.. i can able to get the value from database and converting into json value too but i don't know how to get tat values and display in Front End using(jquery)..

    Thanks
    Naveen

    ReplyDelete
  8. How can i pass the Bean class object using JSON to Servlet . And how to retrieve bean object in Servlet.

    ReplyDelete
  9. hey can you tell me how send data from jsp to android app of theclient

    ReplyDelete
  10. Hi can you please help me "" Example for JSON with Jquery JSP Sending data with clear exlanation""

    ReplyDelete
  11. it's great post for a beginners. it's a most valuable information for a web developers. keep it your sharing. guys if want more information click here school branding uk

    ReplyDelete
  12. Hi sir, very well explained the actual concept of JSP. Keep share more information on this. Thank you!!
    DevOps Online Training

    ReplyDelete
  13. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    Hibernate Training in Electronic City

    ReplyDelete