tags:

views:

339

answers:

1

I created two files 1)index.html 2)player.jsp I am using Tomcat Server. In the index.html i created some checkboxes in a form and assigned the values... After clicking submit i forwarded these values to player.jsp... In player.jsp i dynamically generated an xml file named "generate.xml" . This XML file changes according to the user request. For each new request xml file should be overwritten. if one check box is selected in the form ,structure in generate.xml will be in one form if two check box's are selected in the form ,structure in generate.xml will be of another form. I embedded a flash player in the jsp page which takes generate.xml as input and plays the songs accordingly... The code of the player.jsp is

<%@ page import="java.io.*" %>
<%@ page contentType="text/html;charset=ISO-8859-1" %> 
<%
int iLf = 10;
char cLf = (char)iLf;
String myfile = application.getRealPath("/") + "generate.xml";

File outputFile = new File(myfile);
outputFile.createNewFile();
FileWriter outfile = new FileWriter(outputFile);
outfile.write(" <?xml version='1.0' encoding='UTF-8'?> "+cLf);
outfile.write(" <playlist version='1' xmlns = 'http://xspf.org/ns/0/' > " +cLf);
outfile.write(" <title>My Band Rocks Your Socks</title> "+cLf); 
outfile.write("<trackList>"+cLf); 
%>
 <%! String[] sports; %>
 <%
    sports = request.getParameterValues("sports");

    out.println("<html><body><h1>hello</h1></body></html>");

    if (sports != null)
    { 
         for (int i = 0; i < sports.length; i++)
         { 
              // outfile.writeln (sports[i]); 
           String total=sports[i];
           String[] sa=total.split("[,]");
              // String[] sub=new String();
           outfile.write("<track>"+cLf);
           for (int j=0;j<sa.length;j++)
              {
             // outfile.writeln(sa[j]);
                // outfile.writeln("sa["+j+"]="+sa[j]);
             if( j == 0)
             {
               outfile.write("<location>" + sa[0] +"</location>"+cLf); 
             }
                else if (j == 1)
                     {
                        outfile.write("<image>" + sa[1] +"</image>"+cLf); 
         }
                     else if( j==2)
                          {
                            outfile.write("<title>" + sa[2] +"</title>"+cLf);
                       }

               }// end of inner for loop()       
               outfile.write("</track>"+cLf);
         //outfile.writeln();
      }// end of outer for()
    } 
    //else outfile.writeln ("<b>none<b>");

  outfile.write(" </trackList> "+cLf);
  outfile.write(" </playlist> "+cLf);
  outfile.close();

  %>
<object type="application/x-shockwave-flash" width="400" height="170"
          data="xspf_player.swf?playlist_url=generate.xml">
          <param name="movie" value="xspf_player.swf?playlist_url=generate.xml" />

</object>


my problem is in my local system all this is working fine and the generate.xml is overwritten every time for every new request ... I created ROOT.war and uploaded this file in www.eatj.com here when 1st request is submitted an generate.xml file created according to the request.. for the next request this generate.xml is NOT OVERWRITTEN. The player is taking the generate.xml file generated by the first request for all the new requests made. pls help me in making any changes in the code so that i can overwrite the previous generated xml file..

+1  A: 

The method getRealPath() doesn't work under war, and would return null, AFAIR. You must use relative path in order to make it work. Request.getResourceAsStream() is a better option. The use of getRealPath() is very much discouraged for the very reason.

[Edited]

Found a thread of coderanch confirming my doubt.

Adeel Ansari