Logging in web application deployed as WAR - java

I am coding a Java web web application packaged as war and I would like to add logging and specify the log folder to write log files to (using configuration file, e.g. logback.xml)
Obviously, I would not like to configure the absolute path of the folder. Now I wonder how to configure the log folder in war. What are the best practices and recommended approaches to this?

We use to use relative paths in logback.xml but changed to using an env property. When the path was relative we could never tell the customer exactly where the log file was due to different Java EE server implementations. Using an absolute path with an env variable made it easier. For example
<file>${user.dir}/logs/my_web_app.log</file>

Related

Edit Java Configuration File inside WAR in Tomcat

I compiled a Java application into a WAR file, there is a configuration file inside the WAR file. The configuration file is required to change something after deployed to the production server, because it still contains the UAT server parameters.
However, I don't know where to edit the configuration file after deployed in Tomcat. Please help. Thanks.
It doesn't sound like a correct design. You should -
Load configuration file based on some System parameter (e.g. -Denvironment=UAT or PROD). This will be the decision factor for loading the right configuration file.
Do not package the file inside war itself, if possible externalize it to some other directory where amending is lot easier.

Java webapp - where to put application-generated files?

I'm working on a Java web application that needs to store uploaded files in one directory, and an embedded Neo4j database in another directory. I'm deploying the warfile to Tomcat to serve the application, and the application needs to be runnable under Tomcat in either Linux or Windows.
Where exactly should I be putting these two directories on the host system's filesystem?
I'm confused since I'm accustomed to storing information in databases specified via a URL, etc. Thanks for the help.
Is there a chance webapp have two or more instances running at the same time, say in a same Tomcat with two /path names?
Java has system property user.home you could always create a subfolder on it. Current user is the one running Tomcat server. Print properties to sysout for debug purpose.
Reading a webapp name at runtime you can use servletContext.getRealPath("/") function. You get a filename path to $tomcat/webapps/mywebapp and use last folder entry. Define ServletContextListener in web.xml so you can read webapp name at startup.
Use naming convention ${user.home}/tomcat/${webappname}/ and store any file you please.
Or define a webapp context-param variable in web.xml file and let deployer create an appropriate folder.
http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getInitParameter%28java.lang.String%29

Preventing access to war file without using .htaccess / apache conf

Currently when I deploy a war file to Tomcat it can be downloaded from the URL via something like foo.com/myapp.war.
Most places recommend that you put an entry in a .htaccess file to prevent public access to any war files, or failing that an equivalent entry in your Apache config.
Unfortunately, my host does not provide access to the Apache config (although I can access Tomcat confs) and .htaccess files do not work for all Tomcat/Java related hosting environments. Pretty disappointing. They have been rather unhelpful in this respect.
Without resorting to something like "finding another host" (other than this issue they are fine - I'd rather stay here until my app grows too big), is there anything else I can do to prevent public users accessing my war files, yet still allow Tomcat to deploy the apps when it scans them?
For example, is it possible to specify one directory for Tomcat to scan for war files yet have it deploy the war into the public directories?
Thanks.
It is probably better to ask at https://serverfault.com/. It all boils down to how Tomcat is setup.
The vanilla setup will have a folder called webapps under CATALINE_HOME. You put your WAR archives there (they get auto-extracted and deployed). These folders will not be accessible from HTTP (you cannot download WAR archives from some URL like /webapps/my-test.war). These apps in webapps folder are deployed to some context roots. For example an application my-test.war will by default get deployed as yourhost.com/my-test/.
If you can download your WAR archives from foo.com/myapp.war maybe you can check out what does the CATALINA_HOME/webapp/ROOT app is doing. By default this is deployed under the foo.com. Ask from the host the Tomcat configuration files to figure what kind of custom configurations are they using.
You can place your .war files in any location Tomcat has access to. But you will have to tell Tomcat about it, so it picks them up. You can do this by placing a configuration XML file in
<CATALINA_HOME>/conf/Catalina/localhost/myWebapp.xml
There are samples on what to put into that file myWebapp.xml, e. g. here, step "4)". And of course, the official documentation.

Java web application context

I have a Java web application. Inside the WAR I have a folder containing configuration files for the application. I need to know the path of the folder in order to load the files at runtime.
I also need the solution to work in Tomcat and in WebSphere.
Thanks.
I would suggest placing the files under WEB-INF/classes and simplying loading them from the classpath, not from the filesystem. This way, the path is always the same.
You can use something like:
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("path");

Can I use log4j in a web app with a file other than log4j.xml or log4j.properties?

Is it possible to use something other than the file names log4j.xml or log4j.properties to configure log4j logging in a Java web application?
I want to load a log4j.xml file from a different location on the file path (not in my classpath). Is that possible in a web application using say, JBoss or Tomcat?
You can use PropertyConfigurator.Call configure with file you wanted
Use -Dlog4j.configuration=path/to/your/file.xml startup parameter to specify where your configuration file is. It's the recommended practice anyway:
The preferred way to specify the default initialization file is
through the log4j.configuration system property.
(log4j manual)

Categories