• JSon_零基础_002_将List类型数组转换为JSon格式的对象字符串,返回给界面


    将List类型数组转换为JSon格式的对象字符串,返回给界面

    所需要导入的包:

    编写bean:

    package com.west.webcourse.po;
    
    /**
     * 第01步:编写bean类,
     * 下一步com.west.webcourse.servlet.JavaBeanToJOSNString.java
     */
    public class PersonInfoPo {
        private String name;
        private int age;
        private String sex;
        
        public PersonInfoPo(){}
        
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
        
    }

    编写servlet:

    package com.west.webcourse.servlet;
    
    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 net.sf.json.JSONArray;
    
    public class JavaBeanToJOSNString extends HttpServlet {
    
        /** 第03步:重写doGet()方法,下一步:web.xml */
        @Override
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            System.out.println("转换");
            out.print("转换后的JSON字符串:");
            /** 向浏览器发送JSon格式的字符串 */
            out.print(getSomeOneJSONString());
            out.flush();
            out.close();
        }
    
        /** 第02步:创建JSon格式字符串转换方法 */
        public String getSomeOneJSONString() {
            boolean[] boolArray = new boolean[] { true, false, true };
            JSONArray somesOneJsonArray = JSONArray.fromObject(boolArray);
            System.out
                    .println("com.west.webcourse.servlet.JavaBeanToJOSNString.java::"
                            + somesOneJsonArray);
            return somesOneJsonArray.toString();
        }
    }

    配置: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">
        <display-name></display-name>
        <!-- 第04步:配置servlet,下一步:personinfo.jsp -->
        <servlet>
            <description>This</description>
            <display-name>This</display-name>
            <servlet-name>JavaBeanToJOSNString</servlet-name>
            <servlet-class>com.west.webcourse.servlet.SomeOneJsonString</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>JavaBeanToJOSNString</servlet-name>
            <url-pattern>/javaBeanToJOSNString</url-pattern>
        </servlet-mapping>
    
        <welcome-file-list>
            <welcome-file>personinfo.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

    编写界面:personinfo.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://"
                + request.getServerName() + ":" + request.getServerPort()
                + path + "/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'personinfo.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
    </head>
    
    <body>
        <!-- 第05步:编写界面 -->
        动态获得当前项目的ContextPath所对应的目录:${pageContext.request.contextPath}
        <br>
        <a href="javaBeanToJOSNString">向servlet发送请求</a>
        <br>
    </body>
    </html>
  • 相关阅读:
    备战考研算法笔记(四)快速排序
    VMware安装
    使用 Vagrant 打造跨平台开发环境
    类加载器,注解,动态代理
    IE兼容forEach/map/every/some等新方法
    不得不看的Java代码性能优化总结
    关于Oracle误操作--数据被Commit后的数据回退(闪回)
    JPA与Hibernate的关系
    Jenkins-GitHub-Gradle自动构建项目
    过滤器Filter
  • 原文地址:https://www.cnblogs.com/zjsy/p/4381150.html
Copyright © 2020-2023  润新知