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
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteits basic and good man
ReplyDeleteEla machan.. Thanks...
ReplyDeleteThanks.... Neatly written!!
ReplyDeleteI am getting
ReplyDeletethrow Components.Exception("prompt aborted by user", Cr.NS_ERROR_NOT_AVAILABLE);
above exception
the same thing, can anyone help?
Deletehi
ReplyDeletethis 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 .
Brief, clear, effective.
ReplyDeleteThanks!
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.............
ReplyDeleteSending data is easy.
Deletejavascript 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);
}
thank u for this nice example...
ReplyDeleteawesome sir thanq
ReplyDeletehi 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)..
ReplyDeleteThanks
Naveen
Good job for beginners. Thanks Sameera.
ReplyDeleteHow can i pass the Bean class object using JSON to Servlet . And how to retrieve bean object in Servlet.
ReplyDeletehey can you tell me how send data from jsp to android app of theclient
ReplyDeleteHi can you please help me "" Example for JSON with Jquery JSP Sending data with clear exlanation""
ReplyDeleteThanks !! Helped me a lot!
ReplyDeletethank you so much
ReplyDeletesmart code
Thanks for a great help
ReplyDeleteGood example for beginners
ReplyDeleteit'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
ReplyDeleteVery informative ..i suggest this blog to my friends..Thank you for sharing java training institutes in chennai | java j2ee training institutes in velachery
ReplyDeleteyou made my day thanks brother
ReplyDeleteLove You Mam , You helped like me Messiah
ReplyDeleteHi sir, very well explained the actual concept of JSP. Keep share more information on this. Thank you!!
ReplyDeleteDevOps Online Training
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.
ReplyDeleteHibernate Training in Electronic City
The blog you shared is very good. I expect more information from you like this blog. Thankyou.
ReplyDeleteOracle Training in Chennai | Certification | Online Training Course | Oracle Training in Bangalore | Certification | Online Training Course | Oracle Training in Hyderabad | Certification | Online Training Course | Oracle Training in Online | Oracle Certification Online Training Course | Hadoop Training in Chennai | Certification | Big Data Online Training Course