Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does getRealPath() return null when deployed with a .war file? [duplicate]

getRealPath() is returning the actual path in the local system, but returns null when deployed with a .war file.

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

out.println(myfile);    
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>

Can anyone provide me with an alternative for this? It would be very helpful if you showed some sample code too.

like image 821
musicking123 Avatar asked Feb 11 '09 10:02

musicking123


3 Answers

For a start, ServletRequest.getRealPath(String path) is deprecated. The appropriate replacement is:

ServletContext context = session.getServletContext();
String realContextPath = context.getRealPath(request.getContextPath());

However, the API docs for ServletContext.getRealPath(String path) state:

"This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive)."

So the API is fulfilling its contract! However, all is not lost, as you can load a resource from the WAR using the following method, as defined in ServletContext:

ServletContext context = session.getServletContext();
InputStream is = context.getResourceAsStream("generate.xml");
like image 94
3 revs Avatar answered Oct 18 '22 01:10

3 revs


Bit late, but I came across this question when I was having this issue in WebLogic. My solution was to add this to my weblogic.xml:

<?xml version='1.0' encoding='UTF-8'?>
<weblogic-web-app>
    <container-descriptor>
        <show-archived-real-path-enabled>true</show-archived-real-path-enabled>
    </container-descriptor>
</weblogic-web-app>

I found this solution better for when you don't want to (or can't) edit the configuration on the WebLogic server.

like image 18
Liam Dempsey Avatar answered Oct 18 '22 02:10

Liam Dempsey


do you use Weblogic?

If yes - then this is a Weblogic issue which you may fix in Weblogic admin console ->Domain->Web Applications - click the checkbox "Archived Real Path Enabled".

See: http://ananthkannan.blogspot.com/2009/12/servletcontextgetrealpath-returns-null.html

like image 7
user2094587 Avatar answered Oct 18 '22 02:10

user2094587