Where to put Context.xml in a WAR file? - java

I'm porting a app that ran in tomcat to Jboss where it will be deployed as a .WAR file. I have a line of code that looks for a configuration .xml file :
appContext = new ClassPathXmlApplicationContext("cmdAppContext.xml");
Now I tried putting it in WEB-INF/classes (After comparing with eclipse generated .WAR file), and also tried the WEB-INF folder as it was referred in that location in the web.xml. However each time I get a NullPointerException. Am I missing anything here?
Please note: I've already seen the similar question in forums which was not resolved

It's not a good idea to look into context.xml from source code. You should use a .properties file to configure your app. .WAR is like a .ZIP file with all sites files inside, so META-INF are inside at root folder from .WAR.

Related

Tomcat7 rewrites my context.xml when extracting war file

My java application has more versions of external context.xml, one for each environment. The plan is to have one war file and using the right context.xml for production, staging, dev, etc.
I currently copy/symlink the context.xml into /usr/local/apache-tomcat-7.0.72/conf/Catalina/localhost/myapp.xml. Then I create war file from my sources and I copy it into /usr/local/apache-tomcat-7.0.72/webapps. The catalina is running and explodes the war file in webapps folder. In the same time it deletes my myapp.xml from conf folder. For that reason my app doesn't load the proper configuration and fails. If I copy again the myapp.xml and restart tomcat it works.
Is there a way to tell tomcat to not delete my conf file when extracting war file? Or is there a better place for external context.xml
I guess you have some params in this context.xml for production or stage etc. It's not a good idea to store them there as better approach is to start server with properties and read them in webapp so you never make mistake a.
Look here: https://eureka.ykyuen.info/2009/12/15/tomcat-setting-java-system-properties-for-webapps/

wildfly 8.2 Read properties files inside ear

I need to read a properties file located inside an ear. In jboss 5.1 I put the file in the root of the ear and it works, but in wildfly it doesn´t.
In wildfly, I managed to read the property file outside the ear according to this
http://blog.jyore.com/?p=58
But I haven´t found the way to do the same but with the file inside the ear. I´ve tried without success putting the file in the METAINF folder, also tried with the "Class-Path: ." in the manifest...
Please any ideas?? Do I have to put the file in any specific location??
I found the solution:
In WildFly 8, to get those properties available in the classpath, package them within your application. For example, if you are deploying a .war then package those properties in WAR WEB-INF/classes/ folder. If you want those properties accessible to all components in a .ear, then package them at the root of some .jar and place that jar in EAR lib/ folder.
https://docs.jboss.org/author/display/WFLY8/How+do+I+migrate+my+application+from+AS5+or+AS6+to+WildFly
Thanks for your help

Difference between deploying WAR and Build folder

Question 1: May I ask what is the difference between deploying a java webapp with it's WAR file vs just copy/pasting the build folder into tomcat webapp folder?
Question 2: Somehow I am told to deploy my project just by renaming my /build/web folder to /build/, then copy and paste this folder into tomcat/webapp folder. Tomcat did serve the web app and I could access it via url. But the problem is that I suspect my System variables were not set. I start up a servlet and put this code in this init(ServletConfig config) method:
System.setProperty("LogPath","D:/Test/logs");
And doing this in my log4j.properties
log4j.appender.file.FILE=${LogPath}/wrapper.log
wrapper.log is not found in the designated directory but a stdout.log is found in tomcat/logs folder.
I am sure the init() method was fired because I have a quartz scheduler there. I am suspecting that my System.setProperty was not set. Any hint?
Update: With all the same source code, I have no problem if I am deploying with a WAR file. The ${LogPath} in log4j.properties work as expected.
Let me answer you the first question.
WAR file is a zip archive with different name. When you deploy this file to the Tomcat server, it unpacks this file to its folder as you would do it by copy-paste. If you are just developing your own project in your own environment and you don't want to distribute it, you don't need to create a war file. But if you want to distribute this project, I recommend you to create a war file. One file is easier to be sent.
Read more on Wikipedia http://en.wikipedia.org/wiki/WAR_%28file_format%29

web.xml configuration for JSP

This is the structure of the JSP app on eclipse.Once I run it on eclipse using tomcat server(localhost:8090/index4.html) it works.On the index page i have to add details,this details are uploaded via the servlet as you can see above Java Resources->src->FileUPloadDBServlet(Also I am not sure if it uses this or it uses WEB-INF->src->FileUploadServlet below.)
In my index4.html the action is action=uploadServlet(no address given for it).This everything works on using eclipse.
But once I put the QMS folder(not WAR) from eclipse worskspace to tomcat ROOT,the index4.html works but the following action,i.e uploadServlet doesnt work(here I use the address localhost:8090/WebContent/index4.html)I dont have a web.xml.
Is that creating a problem?
Please provide me help.
You need to put your project folder or .war file in the
webapps folder of Tomcat directly not in ROOT folder under webapps.
Try to follow directory structure as follows
Photo Courtesy http://www.studytonight.com/servlet/steps-to-create-servlet-using-tomcat-server.php (Here you can also find more details on Servlet and JSP, as well as project structure)
Where you have your web.xml put into the WEB-INF folder under your application folder.
For java files you don't need the source files, the classes folder will have all the source folder (src) files compiled and ready to execute.
The lib will contain all the .jar files you need for your application to run.
Make sure you put web.xml file at proper place, because without it, application will not be able to run. Because as they say web.xml is Heart of the application.
Let me explain you the problem.
When we configure a dynamic java project to run on eclipse the server takes the just the stuff from webcontent folder and deploy it the wbcontent folder contains web.xml file which defines the url descriptor for servlet.
now when you copy the whole folder the server can not find the web.xml file which is a descriptor where it expects the file to be.
hope its clear comment for clarification

Java: Accessing properties file inside a war

I already searched StackOverflow for "properties inside war", but none of the results worked for my case.
I am using Eclipse Galileo and GlassFish v3 to develop a set of web services. I am using a "dynamic web project" with the following structure
Src
-java_code_pkg_1
-java_code_pkg_2
-com.company.config
--configfile.properties WebContent
-META-INF
-WEB-INF
--log4jProperties
--web.xml
--applicationContext.xml
--app-servlet.xml
I want to access the "configfile.properties" inside one of the source files in "java_code_pkg1". I am using the Spring Framework and this file will be instantiated once the application starts on the server.
I have tried the following with no luck
getResourceAsStream("/com.company.config/configfile.properties");
getResourceAsStream("/com/company/config/configfile.properties");
getResourceAsStream("com/company/config/configfile.properties");
getResourceAsStream("/configfile.properties");
getResourceAsStream("configfile.properties");
getResourceBundle(..) didn't work either.
Is it possible to access a file when it's not under the WEB-INF/classes path? if so then how?
Properties props = new Properties();
props.load(this.getClass().getResourceAsStream("/com/company/config/file.properties"));
works when I'm in debug mode. I can see the values in the debugger, but I get a NullPointerException right after executing the "props.load" line and before going into the light below it.
That's a different issue. At least now I know this is the way to access the config file.
Thank you for your help.
If you are in a war, your classpath "current directory" is "WEB-INF/classes". Simply go up two levels.
getResourceAsStream("../../com/company/config/configfile.properties");
It is horrible but it works. At least, it works under tomcat, jboss and geronimo and It works today.
P.S. Your directory structure is not very clear. Perhaps it is:
getResourceAsStream("../../com.company.config/configfile.properties");
Check the location of the properties file in WAR file.
If it is in WEB-INF/classes directory under com/company/config directory
getResourceAsStream("com/company/config/configfile.properties") should work
or getResourceAsStream(" This should work if the config file is not under WEB-INF/classes directoy
Also try using getClass().getClassLoader().getResourceAsStream.
Are you sure the file is being included in your war file? A lot of times, the war build process will filter out non .class files.
What is the path once it is deployed to the server? It's possible to use Scanner to manually read in the resource. From a java file within a package, creating a new File("../applications/") will get you a file pointed at {glassfish install}\domains\{domain name}\applications. Maybe you could alter that file path to direct you to where you need to go?
Since you are using Spring, then use the Resource support in Spring to inject the properties files directly.
see http://static.springsource.org/spring/docs/3.0.x/reference/resources.html
Even if the class that requires the properties file is not Spring managed, you can still get access to the ApplicationContext and use it to load the resource
resource would be something like, classpath:settings.properties, presuming that your properties file got picked up by your build and dropped in the war file.
You can also inject directly, from the docs:
<property name="template" value="classpath:some/resource/path/myTemplate.txt">

Categories