Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access file in WebContent folder from a servlet

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.

like image 563
jobinbasani Avatar asked Sep 25 '09 19:09

jobinbasani


1 Answers

To get the path you can just do:

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

s is the class that implements HTTPServlet.You can also use this.getServletContext() if its your servlet class.

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.

like image 67
GBa Avatar answered Oct 13 '22 23:10

GBa