如何使用jsp保存生成的xml文件?

时间:2022-12-03 18:23:46

i have generated xml file using jsp,but i want the generated xml file should be saved as a file. my code is

我使用jsp生成了xml文件,但我希望生成的xml文件应该保存为文件。我的代码是

   <?xml version="1.0" encoding="UTF-8"?>
   <%@ page contentType="text/xml;charset=ISO-8859-1" %>
   <playlist version="1" xmlns = "http://xspf.org/ns/0/">
   <title>My Band Rocks Your Socks</title>
   <trackList>
   <%! String[] sports; %>
   <%
    sports = request.getParameterValues("sports");
    if (sports != null)
    { 
         for (int i = 0; i < sports.length; i++)
         { 
              // out.println (sports[i]); 
              String total=sports[i];
              String[] sa=total.split("[,]");
              // String[] sub=new String();
              out.print("<track>");
              for (int j=0;j<sa.length;j++)
              {
                // out.println(sa[j]);
                // out.println("sa["+j+"]="+sa[j]);
                if( j == 0)
                {
                     out.print("<location>" + sa[0] +"</location>"); 
                }
                else if (j == 1)
                     {
                        out.print("<image>" + sa[1] +"</image>"); 
                     }
                     else if( j==2)
                          {
                            out.print("<title>" + sa[2] +"</title>");
                          }

               }// end of inner for loop()       
               out.print("</track>");
         //out.println();
      }// end of outer for()
    } 
    //else out.println ("<b>none<b>");
  %>
  </trackList>
 </playlist>

1 个解决方案

#1


Yes, you can. The way to do it is basically to set an extra header - which you can do on the response object.

是的你可以。这样做的方法基本上是设置一个额外的标题 - 你可以在响应对象上做。

The header to set is called Content-Disposition and the value should be something like "attachment; filename=\"foo.xml\"".

要设置的标头称为Content-Disposition,值应为“attachment; filename = \”foo.xml \“”。

#1


Yes, you can. The way to do it is basically to set an extra header - which you can do on the response object.

是的你可以。这样做的方法基本上是设置一个额外的标题 - 你可以在响应对象上做。

The header to set is called Content-Disposition and the value should be something like "attachment; filename=\"foo.xml\"".

要设置的标头称为Content-Disposition,值应为“attachment; filename = \”foo.xml \“”。