JSP动态生成XML、json例子

动态生成Xml例子

jsp文件:

 

  1. <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">  
  2.     <jsp:directive.page import="java.text.*" />  
  3.     <jsp:directive.page import="java.lang.*" />  
  4.     <jsp:directive.page contentType="text/xml" />  
  5.     <days>  
  6.     <jsp:scriptlet><!--[CDATA[double compa = 1000.0;  
  7.             double compb = 900.0;  
  8.             for (int i = 0; i <= 30; i++) {  
  9.                 compa += (Math.random() * 100) - 50;  
  10.                 compb += (Math.random() * 100) - 50;]]></jsp:scriptlet>  
  11.     <day>  
  12.     <num><jsp:expression>i</jsp:expression></num>  
  13.     <compa><jsp:expression>compa</jsp:expression></compa>  
  14.     <compb><jsp:expression>compb</jsp:expression></compb>  
  15.     </day>  
  16.     <jsp:scriptlet><![CDATA[}]]--></jsp:scriptlet>  
  17.     </days>  
  18. </jsp:root

 

动态生成的xm结果:

    

  1. <days>  
  2. <day>  
  3.   <num>0</num>   
  4.   <compa>996.86135254584</compa>   
  5.   <compb>903.822919343829</compb>   
  6.   </day>  
  7. <day>  
  8.   <num>1</num>   
  9.   <compa>967.3131498650428</compa>   
  10.   <compb>916.1222681952452</compb>   
  11.   </day>  
  12. <day>  
  13.   <num>2</num>   
  14.   <compa>979.8593089224258</compa>   
  15.   <compb>885.7846657057293</compb>   
  16.   </day>  
  17. <day>  
  18.   <num>3</num>   
  19.   <compa>971.6566309796915</compa>   
  20.   <compb>891.1258613937706</compb>   
  21.   </day>  
  22. <day>  
  23.   <num>4</num>   
  24.   <compa>985.2492393585302</compa>   
  25.   <compb>917.0686871131157</compb>   
  26.   </day>  
  27. <day>  
  28.   <num>5</num>   
  29.   <compa>1033.0987090841088</compa>   
  30.   <compb>927.2036074652575</compb>   
  31.   </day>  
  32. <day>  
  33.   <num>6</num>   
  34.   <compa>1054.5149212850276</compa>   
  35.   <compb>962.4930736736272</compb>   
  36.   </day>  
  37. <day>  
  38.   <num>7</num>   
  39.   <compa>1042.9784745071659</compa>   
  40.   <compb>921.5286937165915</compb>   
  41.   </day>  
  42. <day>  
  43.   <num>8</num>   
  44.   <compa>1060.7260075625763</compa>   
  45.   <compb>943.9500265557228</compb>   
  46.   </day>  
  47. <day>  
  48.   <num>9</num>   
  49.   <compa>1062.4762775909178</compa>   
  50.   <compb>910.7765291821063</compb>   
  51.   </day>  
  52. <day>  
  53.   <num>10</num>   
  54.   <compa>1060.4733494492502</compa>   
  55.   <compb>871.0720342421971</compb>   
  56.   </day>  
  57. <day>  
  58.   <num>11</num>   
  59.   <compa>1067.8778875044777</compa>   
  60.   <compb>891.8903509099296</compb>   
  61.   </day>  
  62. <day>  
  63.   <num>12</num>   
  64.   <compa>1072.8722670108168</compa>   
  65.   <compb>845.4937143999836</compb>   
  66.   </day>  
  67. <day>  
  68.   <num>13</num>   
  69.   <compa>1065.8067735618497</compa>   
  70.   <compb>845.4243750230165</compb>   
  71.   </day>  
  72. <day>  
  73.   <num>14</num>   
  74.   <compa>1065.2344873287916</compa>   
  75.   <compb>874.6165031393934</compb>   
  76.   </day>  
  77. <day>  
  78.   <num>15</num>   
  79.   <compa>1071.0409986108673</compa>   
  80.   <compb>884.3376127467508</compb>   
  81.   </day>  
  82. <day>  
  83.   <num>16</num>   
  84.   <compa>1102.2616685720473</compa>   
  85.   <compb>907.8890712599625</compb>   
  86.   </day>  
  87. <day>  
  88.   <num>17</num>   
  89.   <compa>1126.2560066657747</compa>   
  90.   <compb>872.8460863166563</compb>   
  91.   </day>  
  92. <day>  
  93.   <num>18</num>   
  94.   <compa>1138.3734182460728</compa>   
  95.   <compb>864.0423952070697</compb>   
  96.   </day>  
  97. <day>  
  98.   <num>19</num>   
  99.   <compa>1153.3923198534035</compa>   
  100.   <compb>863.1211125271977</compb>   
  101.   </day>  
  102. <day>  
  103.   <num>20</num>   
  104.   <compa>1107.3005769068625</compa>   
  105.   <compb>907.714705050985</compb>   
  106.   </day>  
  107. <day>  
  108.   <num>21</num>   
  109.   <compa>1066.5875942343364</compa>   
  110.   <compb>938.4965552401181</compb>   
  111.   </day>  
  112. <day>  
  113.   <num>22</num>   
  114.   <compa>1095.6001160921426</compa>   
  115.   <compb>914.7182416967717</compb>   
  116.   </day>  
  117. <day>  
  118.   <num>23</num>   
  119.   <compa>1052.9857629176836</compa>   
  120.   <compb>886.6685365481143</compb>   
  121.   </day>  
  122. <day>  
  123.   <num>24</num>   
  124.   <compa>1083.5315024983206</compa>   
  125.   <compb>893.0866576877667</compb>   
  126.   </day>  
  127. <day>  
  128.   <num>25</num>   
  129.   <compa>1046.4921670055332</compa>   
  130.   <compb>911.3869088223248</compb>   
  131.   </day>  
  132. <day>  
  133.   <num>26</num>   
  134.   <compa>1004.0255991313829</compa>   
  135.   <compb>958.2492188306094</compb>   
  136.   </day>  
  137. <day>  
  138.   <num>27</num>   
  139.   <compa>986.9324045630683</compa>   
  140.   <compb>982.5990401123696</compb>   
  141.   </day>  
  142. <day>  
  143.   <num>28</num>   
  144.   <compa>1014.5262689371409</compa>   
  145.   <compb>951.4555127064398</compb>   
  146.   </day>  
  147. <day>  
  148.   <num>29</num>   
  149.   <compa>979.344421697257</compa>   
  150.   <compb>979.1628945009863</compb>   
  151.   </day>  
  152. <day>  
  153.   <num>30</num>   
  154.   <compa>1003.5417320546833</compa>   
  155.   <compb>997.1723197501847</compb>   
  156.   </day>  
  157.   </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结果

    

javascript] view plaincopyprint?
 
  1. [{"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

  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。