2

I have a maven directory structure:

 /src

    /main

        /java 
        /resources 
        /webapp 

So if I put the file under /resources, when I deploy the application to tomcat the file ends up in: webapps/applicationRoot/WEB-INF/classes/xx.pdf

If I put in webapp/ then it ends up in webapps/applicationRoot/xx.pdf

If I create a folder under webapp like webapp/myResources it ends up in: webapps/applicationRoot/myResources/xx.pdf

The 2nd one looks the prettiest I believe because then in the code I can say:

    InputStream is = servletContext.getResourceAsStream("/re.pdf");

How would I get my pdf file in case 1 in my servlet?

Also, which one is the standart? Which should I use?

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319

2 Answers2

3

If you put your pdf under webapp/bla/my.pdf, a user could directly access it in his browser from http://{yoursercer}/{contextroot}/bla/my.pdf. Is that what you want? Put it there then.

If you want the PDF to be accessible to your Java code, then put it under resources. It will not be directly accessible from a browser.

Assen Kolov
  • 4,143
  • 2
  • 22
  • 32
  • How can I get to it in second option? – Koray Tugay Oct 21 '13 at 05:04
  • What do yo mean? You already said it: if the file is onder resources you can read it from you Java code. Why? I do not know, maybe you want to use it as a template and update it on the fly before returning it in the http response. If you just want to serve it is it is - leave your servlet container do the job and bother no more. Put it under webapp. – Assen Kolov Oct 21 '13 at 11:06
  • No what I am saying is what will be the path I use, the parameter to: getResourceAsStream(); – Koray Tugay Oct 21 '13 at 11:09
  • See the javadoc: http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getResource(java.lang.String) – Assen Kolov Oct 21 '13 at 11:14
  • What I am trying to say is, is there a better way then saying: "WEB-INF/classes/xx.pdf" ? Like some sort of variable or something.. – Koray Tugay Oct 21 '13 at 11:22
  • 1
    if you put it at resources/pdfs/my.pdf, you can access it with MyController.class.getResource("/pdfs/my.pdf"). Pure java, no servlet stuff. – Assen Kolov Oct 21 '13 at 11:31
  • Thanks, this is what I was trying to ask. Thank you. – Koray Tugay Oct 21 '13 at 11:34
  • Sorry I tried but I can not figure out how you got MyController.class.getResource.. What is MyController in here? – Koray Tugay Oct 21 '13 at 21:17
  • Nevermind, I asked: http://stackoverflow.com/questions/19505646/how-does-getresourceasstream-method-in-class-class-work-in-java – Koray Tugay Oct 21 '13 at 22:12
-3

set the content type.response.setContentType("application/pdf");

Jesse
  • 517
  • 5
  • 24