How to pass an argument while deploying a WAR file? - java

I basically need to pass an argument to the WAR file that contains the path to my config.json.
I want to achieve it in such a way so that once the project has been exported as a WAR file, I should be able to change the argument without opening the WAR file and having to export it again.
I understand I can pass arguments in the web.xml which can be obtained in the servlet but wouldn't changing the web.xml require me to open the WAR file?
I am using jetty-runner.jar to deploy my webapp.

When you run jetty-runner.jar, you may pass a proprty to jvm like this
java -jar jetty-runner.jar my.war -Dproperty.name=value
In your web.xml you may use following syntax
<context-param>
<param-name>property.name</param-name>
<param-value>${property.name}</param-value>
</context-param>
See an example here: http://www.xinotes.net/notes/note/1611/

I can see 2 solution to you problem:
1) You define an environment variable that specifies the path to your config.json on the server that hosts you jetty instance
2) You add to the class path the folder that contains the config.json and access it from your web application.

Related

can't run java with spring framework , no named bean [duplicate]

When I run my application on tomcat the spring-context.xml file is located at
/WEB-inf/spring-context.xml
This is ok. But running a junit test I have to supply it with the location of my spring-test-context.xml like this:
#ContextConfiguration(locations={"classpath:/spring-test-context.xml"})
The only way this works is if the file is located in
/src/spring-context.xml
How can I get my application to find my spring-context files in the same location? So that it works with junit testes and deployed on tomcat?
I tried this and it gave me alot of errors about not finding any beans, but it didn't say it couldn't find the file..
classpath:/WEB-INF/spring-test-context.xml
As duffymo hinted at, the Spring TestContext Framework (TCF) assumes that string locations are in the classpath by default. For details, see the JavaDoc for ContextConfiguration.
Note, however, that you can also specify resources in the file system with either an absolute or relative path using Spring's resource abstraction (i.e., by using the "file:" prefix). You can find details on that in the JavaDoc for the modifyLocations() method in Spring's AbstractContextLoader.
So for example, if your XML configuration file is located in "src/main/webapp/WEB-INF/spring-config.xml" in your project folder, you could specify the location as a relative file system path as follows:
#ContextConfiguration("file:src/main/webapp/WEB-INF/spring-config.xml")
As an alternative, you could store your Spring configuration files in the classpath (e.g., src/main/resources) and then reference them via the classpath in your Spring MVC configuration -- for example:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
With this approach, your test configuration would simply look like this (note the leading slash that denotes that the resource is in the root of the classpath):
#ContextConfiguration("/spring-config.xml")
You might also find the Context configuration with XML resources section of the reference manual useful.
Regards,
Sam
(author of the Spring TestContext Framework)
The problem is that /WEB-INF is not in the CLASSPATH for a web app. However, /WEB-INF/classes is.
Your problem with testing is that you aren't running in an app server, so WEB-INF/classes isn't part of the CLASSPATH by default. I'd recommend setting up your tests so that either WEB-INF/classes is in the test CLASSPATH or use a relative or absolute file path to find them.

Rename war file to change the context path of an webapplication

I am building a webapplication with maven. I want to change the name of the generated warfile to get a different context path.
To clarify misunderstandings: It is not about changing the name during development, it should be possible without touching any code (e.g. for customers). Also it should be possible to deploy those war on different servers (like WildFly, Tomcat etc.).
Example:
Hello.war = Hello.war -> URL: localhost:8080/Hello
stupid.war = stupid.war -> URL: localhost:8080/stupid
How can I achieve this? Is that even possible?
For popular servlet containers (JBoss, Tomcat, Jetty), WAR naming convention can drive context paths. Name of the war becomes the context path if no explicit context path is defined anywhere.
a.war > localhost:8080/a
b.war > localhost:8080/b
The problem then is just to rename the war into different names as per your clients.
https://docs.jboss.org/jbossas/guides/webguide/r2/en/html/ch06.html
https://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Naming
http://www.eclipse.org/jetty/documentation/current/configuring-contexts.html
Overriding the default finalName element within the build element to the desired filename (without extension) may archive what you wish. You will then of course need to take care with versions. eg.
<build>
<finalName>YourName</finalName>
</build>
the easy way to do this is
to rename the file extension.war to .zip and expand it (double-click it)
drag out the web.xml to desktop
make changes to web.xml accordingly
drag the modified web.xml to its original location and replace the original
rename the file extension back to .war

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 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

Logging in web application deployed as WAR

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>

Categories