2

I have written a java web service and I need to add a config file to store things like Client Name that will change on a client by client basis. I am looking for something similar to the .net web.config file.

I have tried the web.xml file, but I have been unable to get that to work. Most solutions I've found are to be used in servlets, which I am not using in this project. There was one solution I found that reads the xml file, but it required a Naming exception to be thrown from the method and the web service was not deployable.

I'm a .net developer just starting to learn java. I'm not sure where to look to solve this problem. I'm sure I'm missing something obvious.

Here are some code samples I've played with to try to read the web.xml file.

Context ctx = new InitialContext();
Context env = (Context) ctx.lookup("java:comp/env");
String filename = (String) env.lookup("file-name");

and

String email= getServletContext().getInitParameter("AdministratorEmail");

1 Answers1

0

If you are using Maven as the build tool in your project, you can put the config file in src/main/resources folder. You can also put it in the class path of your application. WEB-INF/lib or WEB-INF classes. You can then easily load the file using ClassLoader.

https://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)

InputStream is= Thread.currentThread()
                .getContextClassLoader()
                .getResourceAsStream(FILE_NAME);

There are many other ways depending on what type of web service and what frameworks/technologies you are using. If you are building a REST service using Spring Boot, you can use application.yml/application.properties to define your config parameters.

kTest
  • 367
  • 1
  • 11
  • Thank you for your reply. When I add this code to my project, it appears to work, meaning there are no compile or rintime errors, but I still have trouble getting the values from the file. –  Jan 12 '18 at 21:10
  • I don't think I ended up using Maven. (I tried a lot of different tutorials before using what I have.) I'm using Netbeans with Tomcat 8. My web service is a SOAP ws. My web.xml file is in the WEB-INF folder. If you have any other thoughts, I'm all ears. What you've given me so far has already been a big help because I was able to extend my searches. Also, I'm trying to get the context-param values, and I'm not even sure that's right. –  Jan 12 '18 at 21:17
  • Can you post the block of code where you are trying to read the config file? Do you have access to ServletContext? If yes try context.getRealPath(“WEB-INF/myconfig.properties”). Then load properties using the file input stream passing the path. – kTest Jan 13 '18 at 01:04
  • Thank you for helping me, the questions you asked me led me down different paths of research, which was a HUGE help. I ended up using the second solution posted here. https://stackoverflow.com/questions/261348/how-can-i-access-the-servletcontext-from-within-a-jax-ws-web-service –  Jan 15 '18 at 22:05