将po(bean)对象集合List转换为JSon格式的对象字符串,返回给界面
导入jar包:
编写:po(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 java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.west.webcourse.po.PersonInfoPo; import net.sf.json.JSONArray; public class JavaBeanToJOSNString extends HttpServlet { PersonInfoPo pif = new PersonInfoPo(); PersonInfoPo pif01 = new PersonInfoPo(); /** 第03步:重写doGet()方法,下一步:测试 */ @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字符串:<br/>"); /** 向浏览器发送JSon格式的字符串 */ out.print(getBeansToJsonString()); out.flush(); out.close(); } /** 第02步:创建JSon格式字符串转换方法:将po对象集合转换成json类型字符串 */ public String getBeansToJsonString() { pif.setAge(1); pif.setName("name"); pif.setSex("sex"); pif01.setAge(101); pif01.setName("name01"); pif01.setSex("sex01"); List lp=new ArrayList(); lp.add(pif); lp.add(pif01); JSONArray beanToJsonArray = new JSONArray().fromObject(lp); System.out.println(beanToJsonArray); return beanToJsonArray.toString(); } }
测试servlet代码:
package com.west.webcourse.servlet; /** * 第04步:测试 * 下一步:配置web.xml */ import org.junit.BeforeClass; import org.junit.Test; public class JavaBeanToJOSNStringTest { static JavaBeanToJOSNString js; @BeforeClass public static void setUpBeforeClass() throws Exception { js=new JavaBeanToJOSNString(); } @Test public void testToJsonString() { js.getBeansToJsonString(); } }
配置web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name></display-name> <servlet> <description>This</description> <display-name>This</display-name> <servlet-name>JavaBeanToJOSNString</servlet-name> <servlet-class>com.west.webcourse.servlet.JavaBeanToJOSNString</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> <!-- 第06步:编写界面 --> 动态获得当前项目的ContextPath所对应的目录:${pageContext.request.contextPath} <br> <a href="javaBeanToJOSNString">向servlet发送请求</a> <br> </body> </html>
时间: 2024-10-26 15:09:56