zoukankan      html  css  js  c++  java
  • 在Servlet中使用JSON

        在Servlet中使用JSON,和上篇的使用相同,只不过多了配置web.xml的内容

        servlet代码如下:

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import net.sf.json.JSONArray;
    
    public class JSONServlet extends HttpServlet {
    
        private static final long serialVersionUID = 1L;
    
        public JSONServlet() {
            super();
        }
    
        public void destroy() {
            super.destroy(); 
        }
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doPost(request, response);
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            //使用JSONArray测试
            JSONArray jsonArray = new JSONArray();
            jsonArray.add("MCA");
            jsonArray.add("kevin");
            jsonArray.add("15-12-1998");
            jsonArray.add(new Double(12.3));
            List<String> list = new ArrayList<String>(); 
            list.add("a collection added");
            list.add("kevin collection test");
            jsonArray.addAll(list);
            
            //页面输出JSONArray的内容
            PrintWriter out = response.getWriter();
            out.print(jsonArray);
            out.println("======================================");
            for(int i=0;i<jsonArray.size();i++){
                out.print(jsonArray.getString(i));
            }
        }
    
        public void init() throws ServletException {
        }
    }

    配置web.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        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">
      <servlet>
        <servlet-name>JSONServlet</servlet-name>
        <servlet-class>com.justinmobile.JSONServlet</servlet-class>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>JSONServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    访问页面

      http://localhost:8080/Ext/test.do

    我们得到输出结果:

    ["MCA","kevin","15-12-1998",12.3,"a collection added","kevin collection test"]====================================== MCAkevin15-12-199812.3a collection addedkevin collection test
  • 相关阅读:
    树莓派 无线网卡配置
    树莓派.Net MVC4 Mono4 Jexus
    springcloud超简单的入门3--负载均衡
    springcloud超简单的入门2--Eureka服务治理
    SpringCloud超简单的入门(1)--一些简单的介绍
    Tomcat9控制台中文乱码的解决方案
    win10 调整音量时 左上角弹框 的解决办法
    .NETCore 添加Docker支持,并上传镜像至Docker Hub,最后在CentOs中拉取镜像运行
    假设每台交换机上行有N条线,两跳内,可以最多让多少个交换机互通?
    .netcore微服务-SkyWalking安装部署IIS
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4741245.html
Copyright © 2011-2022 走看看