0

I am trying to access a conf file (located in the WEB-INF folder) from a Tomcat web app. At the moment, I have the location of the file hard coded as a String. However, this does not work when the tomcat/webapps folder is in a different location than my hard coded String indicates. I've looked online and it seems like using the getResourceAsStream () method is what I'm looking for, but I'm having a hard time getting it to work. My application is not liking it when I call the getServletContext () method. Can anyone help me?

EDIT: The relevant block of code

BufferedReader myReader = new BufferedReader (new InputStreamReader (getServletContext ().getResourceAsStream ("/WEB-INF/conf.txt")));
Alfabravo
  • 7,493
  • 6
  • 46
  • 82
Dan Forbes
  • 2,734
  • 3
  • 30
  • 60
  • 1.) Not liking =? what error. 2.) How does your code look like? – Alfabravo Aug 22 '12 at 21:22
  • 1
    And the compiler says... – Raffaele Aug 22 '12 at 21:24
  • We really can't help you if you don't post the code – Chris Thompson Aug 22 '12 at 21:26
  • `BufferedReader myReader = new BufferedReader (new InputStreamReader (getServletContext ().getResourceAsStream ("/WEB-INF/conf.txt")));` >> "The method getServletContext() is undefined for the type ..." – Dan Forbes Aug 22 '12 at 21:30
  • Still having a really hard time w/ this. I think part of the problem is that I am trying to access this information from a POJO running on a Tomcat servlet. Can I get the servletContext from an MBean running on my Tomcat servlet? – Dan Forbes Aug 24 '12 at 20:53

4 Answers4

2

To solve this problem I created two folders under WEB-INF (path/to/app/WEB-INF/classes/mypackage/) and then put my files in this folder. Then, from my POJO I called this.getClass.getResourceAsStream ("<filename>") to open up a stream. To just get a String that was the complete absolute path name of a file I did this.getClass.getResource ("<filename>").toString ().substring (5).

Dan Forbes
  • 2,734
  • 3
  • 30
  • 60
1

If you are trying to load a file from the WEB-INF directory in Tomcat, use the following code:

For example, for a file in WEB-INF/config/config.xml

ServletContext context = ....//get servlet context
InputStream is = context.getResourceAsStream("/WEB-INF/config/config.xml");
Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
  • Any suggestions on how to go about getting the servlet context? I think that is the crux of my problem. – Dan Forbes Aug 22 '12 at 21:30
  • Furthermore, Eclipse (the IDE I'm using) doesn't even recognize Objects of the type ServletContext. `ServletContext thisContext;` >> "ServletContext cannot be resolved to a type" with suggestions that seem suitable. – Dan Forbes Aug 22 '12 at 21:39
  • Make sure your class is extending GenericServlet (e.g. HttpServlet) so you have the `getServletContext()` method available. Also make sure the Apache Tomcat library is in your project's build path in Eclipse; if not, right click your project -> Build Path -> Add Library -> Server Runtime -> Apache Tomcat. –  Aug 22 '12 at 22:06
  • request.getServletContext, sir – Alfabravo Aug 22 '12 at 22:07
0

If your config file is at /WEB-INF/config.xml, you could access it from a servlet by using "../config.xml" instead of a full path. This works because compiled application classes are normally located within /WEB-INF/classes.

Brian Showalter
  • 4,321
  • 2
  • 26
  • 29
  • Thank you for the response. I was really hoping this would work as it is the simplest solution, but it resulted in the following error, "java.io.FileNotFoundException: ../config.txt (No such file or directory)" – Dan Forbes Aug 24 '12 at 15:10
0

Following code worked for me. I kept my config file under WEB-INF/

web.xml code snippet:

<servlet>
    <servlet-name>wizardcontroller</servlet-name>
    <servlet-class>com.sg.poc.wizard.controller.CustomerWizardController</servlet-class>
    <init-param>
        <param-name>plm-config</param-name>
        <param-value>plm-config.xml</param-value>
    </init-param>
</servlet>

Servlet code snippet:

String configfile = config.getInitParameter("plm-config") ;
InputStream is = config.getServletContext().getResourceAsStream("/WEB-INF/"+configfile);
System.out.println("Resource Stream is : "+is);
Nazik
  • 8,696
  • 27
  • 77
  • 123
Murali
  • 1