tags:

views:

784

answers:

2

Hi,

I'm trying to generate a PDF document using FOP. The pdf generation code is kept in a servlet and the xsl is in a specific folder in the WebContent folder.

How can I access this xsl file by giving a relative path? It works only if I give the complete path in the File object.

I need to generate the xml content dynamically. How can I give this dynamically generated xml as the source instead of a File object?

Please provide your suggestions.

+1  A: 

To get the path you can just do:

String path = s.getServletContext().getRealPath("/WEB-INF/somedir/hdfeeh");

Then pass this as a parameter.

As far as using dynamically generated XML, the library you're using should support using an input stream, write your XML, convert it to a byte array, then wrap it in a ByteArrayInputStream and use this.

Greg
+1  A: 

For a direct and independent container implementation, you can access the resourcewith the following method getResource() inside your servlet:

/start servlet/

public InputStream getResource(String resourcePath) {
  ServletContext servletContext = getServletContext();
  InputStream openStream = servletContext.getResourceAsStream( resourcePath );
  return openStream;
}

public void testConsume() {
  String path = "WEB-INF/teste.log";
  InputStream openStream = getResource( path );

  int c = -1;
  byte[] bb = new byte[1024];
  while ( -1 != ( c = openStream.read( bb ) ) ) {
    /* consume stream */
  }
  openStream.close();
}

/end servlet/

apast
Unfortunately I'm not able to load the resource into stream.I get the exception java.net.MalformedURLExceptionI printed servletContext and its shown as com.ibm.ws.webcontainer.facade.ServletContextFacade@36c29971Any ideas?
whoopy_whale
After using servletContext.getRealPath(filePath); its working fine :)
whoopy_whale
Here, the code work fine.I'm using Tomcat 6.*.Now I see that you are using IBM platform. Maybe, that's the difference between results.
apast