JSON and Servlet example
来源:互联网 发布:假装情侣聊天软件 编辑:程序博客网 时间:2024/06/05 00:49
JSON and Servlet example
In the previous section of JSON-Java example youhave learned how to create a java class by usingJSON classes. Now inthis example we will tell you how to use JSON classes for creating Servlet.
In this example we have created an object of JSONArrayand then we have added elements into this array by using the methodadd().To have functionality of JSON in your application you must have JSON-lib andits supported jar files. These are:commons-lang.jar,commons-beanutils.jar,commons-collections.jar,commons-logging.jar,ezmorph.jarand json-lib-2.2.2-jdk15.jar.
Here is the example code of JSONServlet.java asfollows:
JSONServlet.java
import java.io.*;
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("MCA");
arrayObj.add("Amit Kumar");
arrayObj.add("19-12-1986");
arrayObj.add(24);
arrayObj.add("Scored");
arrayObj.add(new Double(66.67));
PrintWriter out = response.getWriter();
out.println(arrayObj);
for(int i=0;i<arrayObj.size();i++){
out.println(arrayObj.getString(i));
}
}
}
We have to do corresponding servlet mapping into web.xmlas given below:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>JSONServlet</servlet-name>
<servlet-class>JSONServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JSONServlet</servlet-name>
<url-pattern>/JSONServlet</url-pattern>
</servlet-mapping>
</web-app>
To run this example follow this step by step procedure:
- create a JSONServlet.java class
- compile JSONServletand place it into WEB-INF/classes directory
- Download JSONLibraries and place it into Tomcat's libdirectory
- Start Tomcat Webserver and type
http://localhost:8080/JSON/JSONServlet into browser's address bar you will have following output on your browser.
Output:
Download JSON-Servlet Project code
- JSON and Servlet example
- Spring 3 MVC and JSON example
- JSON.simple example – Read and write JSON
- JSON.simple example – Read and write JSON Read
- AJAX with JSP and Servlet using Jquery Example
- json example
- JSON example
- Java Servlet Example
- Json Format example
- [Home] JSON-C Example
- SpringMVC RESTful Json Example
- JSON Models Example
- Go by Example: JSON
- Spring REST Client with RestTemplate: Consume RESTful Web Service Example for XML and JSON
- Spring REST Client with RestTemplate: Consume RESTful Web Service Example for XML and JSON
- Makefile and Example
- @PostConstruct and @PreDestroy example
- hashCode() and equals() Example
- Java SE 6 Hotspot [TM] 虚拟机垃圾回收调优
- MyEclipse自动补全与快捷键设置
- Oracle中临时表的深入研究
- 蓦然回首 那足球 2011
- Memcached缓存技术
- JSON and Servlet example
- PL SQL基本语法要术
- c_str
- PL/SQL简介
- Struts 2 Ajax Tutorial with Example
- 黑马程序员_udp
- Flash之死
- VS2008下的CListCtrl控件的使用
- 下一代网页:当HTML5取代Flash