I have created a dynamic web project, and use Apache Tomcat as a server.
In my servlet I'm creating a text file and want to reuse that in a JSP. However they are by default created in the installation folder of Eclipse when I do something as simple as the following:
File f = new file("test.txt").
I don't know why this happens. Is there a way to create the file in the WebContent directory as I want to make that file available for download in my JSP.
Java has a concept of the "current directory". When you start an application via Eclipse, this may indeed point to your installation directory. If you don't specify any path, a file will be created in this current directory. Hence the reason why your test.txt ends up there.
The WebContent directory is a something that is specific to Eclipse. Your code should not depend on putting anything there. You only start your application via Eclipse when you're developing it, not when you're deploying it to a live server.
The content of this directory will become the root of your .war, which is a well known location independent of how you start & deploy you app, BUT you still cannot depend on writing anything to this location at run-time. You might deploy your application as a packaged .war (likely for live deployments) or you may deploy your application unpackaged but then your application server may simply not pick up any changes done at run-time.
What you can do if you are sure your application only runs on a single server is writing the files to a well known location on your file system, such as /tmp, or /var/yourapp/files, etc. The code serving up those files can then pick them up from that location.
If you want to play it 100% safe according to the Java EE rules, you'd store your files on something like an FTP server that has a configurable address. Technically your war could be shipped between nodes on a cluster and requests could end up going to different machines, so depending on a local filesystem wouldn't work then.
Executing this statement this.getServletContext().getRealPath (""), you'll obtain the path where Tomcat WebServer is pointing at at runtime. You could add a folder "MyFolder" and call this statement:
new File(this.getServletContext().getRealPath ("") + "/MyFolder/test.txt");
Anyway, the default path looks something like:
...\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\<NameOfYourProject>
Note that when you create a new file, it won't appear in your immediate workspace (check the .metadata path), unless you change the runtime location tomcat should point at.
Related
I am writing a Quarkus application which reads data over http. In my application.properties file, I have this line:
my.resource=http://path/to/file
Every time I run the app, it has to download the file so I created a smaller version of the file locally for developing purpose. The problem is that I don't know how to put it in the properties file.
Ideally, I want something like this:
my.resource=http://path/to/file
%dev.my.resource=file://${project-dir}/sample_data/file
And I have to use the absolute path because I used new URI(resource).toURL() method which requires an absolute URI.
Thanks in advance.
Application properties is something that is used when your application is deployed to adopt your application to the target environment, does the user of the deployed application know anything about project directory? Project directory is something that makes sense when you are developing your application. having said that using project directory in that file does not make sense at all.
I am facing a little strange issue while deploying web service to WAR file.
If I deploy the application via Netbeans IDE it is going under \standalone\deployments directory.
However, if I deploy the war file from Admin Console it is always getting deployed at \standalone\tmp directory.
Please guide on this issue. The deployment should go under \standalone\deployments directory only.
The deployment should go under \standalone\deployments directory only
You are quite not right.
It is not an issue. It is what it is.
standalone/deployment folder stand there only for "hot-deployment" functionality available only with standalone mode.
So, Netbeans uses it. You can do the same just by saving EAR or WAR into standalone/deployment and server will pick it. (default scan interval is 5 sec.)
but Admin console or CLI is only (and standard) way to deploy application on domain. In domain mode deployment folder is not in use and there is no deployment scanner.
Then when you use console it goes common way - deploys as on domain regardless is it domain or standalone server.
Updated / follow-up:
In general it is better to keep .properties file(s) out of deployment, in separate location. It is main idea behind them - to be able to change properties without application rebuilding and redeploying. Usually properties are different in different environments (DEV/UAT/PROD)
So there are 2 most popular solutions:
store properties in different location add that location to class path and access them through ClassLoader.getResourceAsStream() mechanism
store properties in different location, pass that location through system (or -D) variable and access them as file. for JBoss you can place your .properties under configuration directory. there is already JBoss variable. Kind of jboss.config.dir (or such, you can find it in Admin console, I do not have JBoss right now).
But of course sometime it still needed to access resources inside WAR/EAR - in that situation it is pretty much the same as first solution above.
Just be sure your .properties file(s) are accessible through to ClassLoader (in class path) and use them from ClassLoader.getResourceAsStream (or if you use Spring point it as "classpath:" not as "file:".
I have a configuration project that few projects are using it.
All of my project under the same workingspace.
workingspace/configuration
workingspace/webapp1
workingspace/mongoDB
workingspace/model
mongoDB and the model project are using the configuration jar project and able to read the xml files using relative path ../configuration/conf/....xml
when using the
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
inside both of them, I'm getting the correct path (workspace)
while trying to do it from the web app servlet i'm getting the folder of eclipse.exe that causing some problems.
How can i fix the user.dir defalte path for the web app?
At runtime, e.g. when you're not running from inside eclipse, you probably want to work in a defined directory as well - I'd suggest to explicitly configure a specific directory. When you're running within an appserver, you might run as an unpriviledged user that doesn't have a home directory writeable at all (when the account is properly administered with minimal permissions). This differs from applications that are launched ad hoc. In fact, you probably can't assume that your application server runs as the same user as your standalone apps do.
Another alternative is to utilize the system's temp directory - if these are truly temporary files. This could be assumed writeable, or complaints if it isn't do make sense.
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
I have a web application deployed in WebLogic. In one of my java file, I tried to read PleaseNote.txt as following:
File file = new File("PleaseNote.txt");
Now WebLogic is taking PleaseNote.txt from its domain directory.My question is:
Why it is domain directory? Why not the directory where my java file which has the above line of code is in?
Is there any configuration which I am not aware of , but did unknowingly, for WebLogic to look in its domain directory?
What are the implications / side effects of using above line of code in production?
Any WeLogic experts, please respond.
Thank you
Regards
Chaitanya
Reading a file using that way makes your application less portable and not very robust: if you deploy your application on another application server, you'll have to find out where to put that PleaseNote.txt file again or the code will break.
This breaks the WORA (Write Once, Run Anywhere) principle and I'd consider this as a bad
practice.
So, I'd rather put this file in the classpath and use ClassLoader#getResourceAsStream(String name) to read it.
It is domain directory because it's corresponds to value of user.dir system variable, the place where java reads/writes files if path not explicitly set.
Why domain directory corresponds to user.dir ? Because you start Weblogic server here.
Regards
Alexander Rozhkov
when using new File(..) java looks for the file in the directory from where java.exe is started. In case of an weblogic domain, this is ofcourse the domain directory. This is default java behaviour.
When you want to load a file that is in de same directory as the class-file you are loading from, use ClassLoad.getResourcesAsStream(). If you want to load a resource from the classpath use the same method, but prefix your file with "/".