8

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.

jobinbasani
  • 2,075
  • 6
  • 47
  • 66

3 Answers3

19

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.

avck
  • 3,535
  • 3
  • 26
  • 38
GBa
  • 17,509
  • 15
  • 49
  • 67
  • 4
    Don't quite get it. I don't know what is `s`? – newbie Aug 24 '14 at 08:38
  • 2
    @newbie `s` is an object that implements [`ServletConfig`](http://docs.oracle.com/javaee/5/api/javax/servlet/ServletConfig.html). `HttpServlet` extends a class that implements `ServletConfig`. This means that if your class extends `HttpServlet`, you can do `this.getServletContext()`. – Austin Moore Mar 11 '15 at 00:29
  • it doesn't work for me. if I check file exists with such path, it will return false. have a look at my question: http://stackoverflow.com/questions/36598562/unable-to-access-file-in-web-inf-folder-from-servlet – Thang Do Apr 13 '16 at 12:44
6

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/

Andre Pastore
  • 2,841
  • 4
  • 33
  • 44
  • Unfortunately I'm not able to load the resource into stream. I get the exception java.net.MalformedURLException I printed servletContext and its shown as com.ibm.ws.webcontainer.facade.ServletContextFacade@36c29971 Any ideas? – jobinbasani Sep 25 '09 at 19:44
  • 1
    After using servletContext.getRealPath(filePath); its working fine :) – jobinbasani Sep 25 '09 at 20:01
  • 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. – Andre Pastore Sep 25 '09 at 20:34
1

I used the following method to read the file under web content

BufferedReader reader = new BufferedReader(new InputStreamReader(request.getSession().getServletContext().getResourceAsStream("/json/sampleJson.json")));

Now all the file content is available in the reader object.

Rajan
  • 111
  • 6