JSP动态生成XML、json例子
动态生成Xml例子
jsp文件:
- <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
- <jsp:directive.page import="java.text.*" />
- <jsp:directive.page import="java.lang.*" />
- <jsp:directive.page contentType="text/xml" />
- <days>
- <jsp:scriptlet><!--[CDATA[double compa = 1000.0;
- double compb = 900.0;
- for (int i = 0; i <= 30; i++) {
- compa += (Math.random() * 100) - 50;
- compb += (Math.random() * 100) - 50;]]></jsp:scriptlet>
- <day>
- <num><jsp:expression>i</jsp:expression></num>
- <compa><jsp:expression>compa</jsp:expression></compa>
- <compb><jsp:expression>compb</jsp:expression></compb>
- </day>
- <jsp:scriptlet><![CDATA[}]]--></jsp:scriptlet>
- </days>
- </jsp:root>
动态生成的xm结果:
- - <days>
- - <day>
- <num>0</num>
- <compa>996.86135254584</compa>
- <compb>903.822919343829</compb>
- </day>
- - <day>
- <num>1</num>
- <compa>967.3131498650428</compa>
- <compb>916.1222681952452</compb>
- </day>
- - <day>
- <num>2</num>
- <compa>979.8593089224258</compa>
- <compb>885.7846657057293</compb>
- </day>
- - <day>
- <num>3</num>
- <compa>971.6566309796915</compa>
- <compb>891.1258613937706</compb>
- </day>
- - <day>
- <num>4</num>
- <compa>985.2492393585302</compa>
- <compb>917.0686871131157</compb>
- </day>
- - <day>
- <num>5</num>
- <compa>1033.0987090841088</compa>
- <compb>927.2036074652575</compb>
- </day>
- - <day>
- <num>6</num>
- <compa>1054.5149212850276</compa>
- <compb>962.4930736736272</compb>
- </day>
- - <day>
- <num>7</num>
- <compa>1042.9784745071659</compa>
- <compb>921.5286937165915</compb>
- </day>
- - <day>
- <num>8</num>
- <compa>1060.7260075625763</compa>
- <compb>943.9500265557228</compb>
- </day>
- - <day>
- <num>9</num>
- <compa>1062.4762775909178</compa>
- <compb>910.7765291821063</compb>
- </day>
- - <day>
- <num>10</num>
- <compa>1060.4733494492502</compa>
- <compb>871.0720342421971</compb>
- </day>
- - <day>
- <num>11</num>
- <compa>1067.8778875044777</compa>
- <compb>891.8903509099296</compb>
- </day>
- - <day>
- <num>12</num>
- <compa>1072.8722670108168</compa>
- <compb>845.4937143999836</compb>
- </day>
- - <day>
- <num>13</num>
- <compa>1065.8067735618497</compa>
- <compb>845.4243750230165</compb>
- </day>
- - <day>
- <num>14</num>
- <compa>1065.2344873287916</compa>
- <compb>874.6165031393934</compb>
- </day>
- - <day>
- <num>15</num>
- <compa>1071.0409986108673</compa>
- <compb>884.3376127467508</compb>
- </day>
- - <day>
- <num>16</num>
- <compa>1102.2616685720473</compa>
- <compb>907.8890712599625</compb>
- </day>
- - <day>
- <num>17</num>
- <compa>1126.2560066657747</compa>
- <compb>872.8460863166563</compb>
- </day>
- - <day>
- <num>18</num>
- <compa>1138.3734182460728</compa>
- <compb>864.0423952070697</compb>
- </day>
- - <day>
- <num>19</num>
- <compa>1153.3923198534035</compa>
- <compb>863.1211125271977</compb>
- </day>
- - <day>
- <num>20</num>
- <compa>1107.3005769068625</compa>
- <compb>907.714705050985</compb>
- </day>
- - <day>
- <num>21</num>
- <compa>1066.5875942343364</compa>
- <compb>938.4965552401181</compb>
- </day>
- - <day>
- <num>22</num>
- <compa>1095.6001160921426</compa>
- <compb>914.7182416967717</compb>
- </day>
- - <day>
- <num>23</num>
- <compa>1052.9857629176836</compa>
- <compb>886.6685365481143</compb>
- </day>
- - <day>
- <num>24</num>
- <compa>1083.5315024983206</compa>
- <compb>893.0866576877667</compb>
- </day>
- - <day>
- <num>25</num>
- <compa>1046.4921670055332</compa>
- <compb>911.3869088223248</compb>
- </day>
- - <day>
- <num>26</num>
- <compa>1004.0255991313829</compa>
- <compb>958.2492188306094</compb>
- </day>
- - <day>
- <num>27</num>
- <compa>986.9324045630683</compa>
- <compb>982.5990401123696</compb>
- </day>
- - <day>
- <num>28</num>
- <compa>1014.5262689371409</compa>
- <compb>951.4555127064398</compb>
- </day>
- - <day>
- <num>29</num>
- <compa>979.344421697257</compa>
- <compb>979.1628945009863</compb>
- </day>
- - <day>
- <num>30</num>
- <compa>1003.5417320546833</compa>
- <compb>997.1723197501847</compb>
- </day>
- </days>
动态生成json例子
jsp
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2"> <jsp:directive.page import="java.text.*"/> <jsp:directive.page import="java.lang.*"/> <jsp:directive.page contentType="text/json"/> [<jsp:scriptlet> <!--[CDATA[ double compa = 1000.0; double compb = 900.0; for (int i = 0; i<=30; i++) { compa += ( Math.random() * 100 ) - 50; compb += ( Math.random() * 100 ) - 50; if ( i > 0 ) out.print( "," ); ]]> </jsp:scriptlet>{"compa":<jsp:expression>compa</jsp:expression>,"compb":<jsp:expres sion>compb</jsp:expression>}<jsp:scriptlet> <!
json结果
- [{"compa":1023.0359996021378,"compb": 890.6336627306306} ,{"compa":981.6439460583431,"compb": 911.219703971831} ,{"compa":969.8497330126237,"compb": 861.6488260146645} ,{"compa":958.5570762322485,"compb": 824.4960352261958} ,{"compa":929.4895696592979,"compb": 788.2138614159395} ,{"compa":978.3883319861504,"compb": 808.9312883173757} ,{"compa":1010.8893401592406,"compb": 845.4662409118785} ,{"compa":1045.2566149975687,"compb": 806.3813507928484} ,{"compa":1037.6176128323675,"compb": 816.0175191279741} ,{"compa":994.5533831638427,"compb": 790.9237293546263} ,{"compa":1037.5786687444636,"compb": 822.3398801625336} ,{"compa":1029.2318823698392,"compb": 827.6988653853997} ,{"compa":1000.0549925650002,"compb": 861.8150832049502} ,{"compa":988.9622828171528,"compb": 856.9062010099125} ,{"compa":1007.6308555468398,"compb": 837.3690338187375} ,{"compa":992.3857755181227,"compb": 824.006188574049} ,{"compa":944.4879531494171,"compb": 857.1387569515648} ,{"compa":988.31222152331,"compb": 882.0195751566719} ,{"compa":981.3982918042382,"compb": 869.8376759147557} ,{"compa":980.3253748605326,"compb": 862.4756387823064} ,{"compa":947.7066027426299,"compb": 864.6492876691858} ,{"compa":957.1368513546369,"compb": 904.2918074977754} ,{"compa":927.9443314784216,"compb": 951.8390594770885} ,{"compa":974.415053103257,"compb": 1000.9181108471189} ,{"compa":971.7000101867241,"compb": 1009.0791744881171} ,{"compa":998.9580927185284,"compb": 1031.701225794589} ,{"compa":986.2362372491122,"compb": 984.9811431491862} ,{"compa":949.2303460350654,"compb": 939.8645667371009} ,{"compa":998.3417779426563,"compb": 951.7767181134687} ,{"compa":953.6907679144605,"compb": 963.0128012507109} ,{"compa":931.2884698065524,"com
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。