I've created a web application using Netbeans. Before, when I was running the web app via netbeans and tomcat server (which was a zip), all my external files (uploaded files and other helper files I use for my app) are stored in bin directory.
Now, I tried installing an apache tomcat service using windows installer because I wanted to know how to deploy the project on a dedicated server. I have successfully deployed the war file using tomcat's deploy utility. However, when I run the project via the apache tomcat windows service, it is not saving the files in bin dir and it cannot read my files that I pasted in bin dir, too.
Where do you think should I place my files?
EDIT: Upon observing the tomcat service directory, I found out that it is store in the root. If I have my tomcat installed at 'E:\Apache\services\tomcat\', it is stored at the 'tomcat' directory.
Ultimately, it is what your application does that determines where the files are stored.
By the sounds of it, your application is storing files in the current directory of the JVM, which happens to be the "bin" directory when you launch the web server via NetBeans. If so, you will find them, in whatever the current directory is when Tomcat is launched as a windows service.
Frankly, I think you've got this wrong. You should be making a conscious decision as to were uploaded files should be stored, and then making sure that the upload mechanism you are using puts them there.
Obviously, putting them in the current directory is a bad idea. You don't want them being stored in different places depending on how the web container is started. And obviously the "bin" directory is an inappropriate place. (What happens if the user tries to upload a file whose name matches one of the scripts that live in "bin"?)
So where should you be putting the files?
In my opinion, you've got three choices:
In a subdirectory of the work directory ... which is where Tomcat conventionally puts transitory files such as compiled JSPs.
In a custom subdirectory of the Tomcat installation directory.
In a separate directory somewhere else in the file system.
You shouldn't be dropping then in the webapp directory, because files there are typically blown away when the webapp is redeployed, and because there's a greater risk that uploaded files will interfere with your webapp.
You shouldn't be dropping them in the bin or logs or lib or config directories because of the risk of interference ... and because they are simply not the logical place.
If you want to write files relative to the root of the tomcat installation directory, you can find out what that is by calling System.getProperty("catalina.base").
But what ever you do, you need to make sure that a user can't accidentally or deliberately upload files to the wrong place; e.g by supplying an absolute pathname, or a pathname that uses "../../...." to escape from your upload area.
When you install Apache, the project should be inside the webapp folder :
C:\Apache\tomcat\webapps
Like my Project is gaganisonline so the directory structure is something like this :
Path : C:\Apache\tomcat\webapps\gaganisonline
gaganisonline
| |
WEB-INF index.html
|
---------------------------
| | | |
web.xml src lib classes
Related
Can anyone help me understand the classpath logic when deploying Java applications to remote hosts?
Netbeans will build, deploy and execute my Java application correctly on a remote Linux (Ubutntu 20.x) host.
Lets say that the executable JAR is deployed and executed in:
/home/user/project/dist
With any supporting library files copied to:
/home/user/project/dist/lib
This all makes sense to me.
However, I wish to read application and log4j2 configuration files. I would think that these should be placed in the same directory as the jar file. However they must be placed in the parent directory:
/home/user/project
Okay... BUT...
If I amend my code to write out a text file (to the executable directory) the resulting text file is written to:
/home/user/project/
<Edited 19Jun22>
I want my deployed application to read the log4j2.xml and configuration files from the same directory that my application writes to. What configuration settings must I change in Netbeans?
Alternatively is there a way to make Netbeans deploy to /home/user/project instead of /home/user/project/dist?
I have a maven web project in eclipse. I need to get the project's path, actually have to get list of files under src\main\resources\someFolder in project.
Tried String dataDir = "src\\main\\resources\\someFolder";, on running this directory structure is created inside eclipse folder like F:\softwares\eclipse\eclipse\src\main\resources\someFolder. Same when using / instead of \\.
Tried System.getProperty("user.dir")and new File(".").getAbsolutePath(), they return F:\softwares\eclipse\eclipse.
I need to access the project folder in my workspace F:\workspace\Project\src\main\resources\someFolder
But when created a core java app and used System.getProperty("user.dir")and new File(".").getAbsolutePath(), I am getting project path in workspace, F:\workspace\Project. This src\\main\\resources\\someFolder also works fine then.
Why this odd behavior from eclipse?
As mentioned here the directory user.dir is the place where the JVM is started. As web applications are mostly jar/war/ear packages placed somewhere within the folder of the server eclipse handles them in a different way because the behaviour of such a web application is different. You cannot expect to have file access from outside the jar/war/ear file. Within the jar/war/ear file everything from within src/main/resources will be available just by using getResourceAsStream as described in many other stackoverflow articles. This way you mustn't use src/main/resources/myfile.txt but myfile.txt.
Don't try to guess or use what the user.dir / JVM/server start folder is!
I am creating a JSF application using Netbeans and i had a download folder called Snap and Files which stores the client uploaded Snaps and Files respectively but when i clean the project whole build folder gets deleted and so my files. Is there any way to prevent it from deleting some folder or any alternative.(I dont want to save it on any static path.)
You can and should not store the uploaded files in expanded WAR folder. They would in a real production environment also get lost simply because files which are added during runtime are not contained in the original WAR.
You should store them on a fixed path outside the deploy folder. There are several ways to configure the webapp to use it and to configure the server to serve files from the folder by a virtual path. See also this answer for a detailed explanation and examples: Uploaded image only available after refreshing the page
I have tomcat installed at "C:\Program Files\apache-tomcat-7.0.27"
I have eclipse installed at "C:\Program Files\eclipse"
And I have the workspace located at "C:\workspace"
I'm using "Java perspective", created a "Java Project" with the default output folder as "helloWorld/web/WEB-INF/classes".
The structure of the project goes like this:
-helloWorld
---src
-----servlets
-------hello.java
-------world.java
---web
-----WEB-INF
-------jsp
---------hello.jsp
---------world.jsp
-------lib
-------web.xml
---helloWorld.xml
---record.txt
doPost() in hello.java generate a random number, and write it to a text file "record.txt".
doPost() in world.java open the text file "record.txt" and read a number.
The system is working, but what I originally put in the record.txt file in eclipse project never get changed, and I'm sure that what world.java read from the file is exactly what hello.java generated.
I checked "C:\Program Files\apache-tomcat-7.0.27\work\Catalina\localhost\helloWorld", and only jsp files are there.
I then tried restart tomcat and reload and even undelopy and deploy again, but the previous generated number is still there. I didn't try restart computer.
My question is where is the record.txt file? It is definitely not the one in the eclipse project.
If you use a relative file path in your Java code then it will not be relative to your webapp it will be relative to where the process running Tomcat was started. Therefore you might find your file in the Tomcat bin directory or somewhere similar.
If you want to create a file relative to your webapp then you need to obtain the path to your webapp, which you can do by calling getServletContext().getRealPath("") in your Servlet.
Eclipse deployed projects are run in temp folder. the path looks some thing like this... tmp0/conf/catalina/localhost/projectname.....context.xml of servers might help you in this..
i suggest u should go for an absolute path....i faced same problem while using eclipse...That time i had to even provide the link to the user...
Check your webapps folder for a folder with the same name as your project.
C:\Program Files\apache-tomcat-7.0.27\webapps
Here you will find the exact folder structure and your record.txt file as well.
Hope this helps!
Is there a workaround or a solution to having to place the javax.comm.properties file and the win32com.dll file in their respective folders?
My program works fine when I have the files stored as below:
%JAVA_HOME%/jre/lib/ext/comm.jar
%JAVA_HOME%/bin/win32com.dll
%JAVA_HOME%/lib/javax.comm.properties
This worked well until IT changed the permissions on our computers so that we can no longer write to these folders. I'd like to be able to install the Java program I wrote that uses the serial port once without needing to re-install it every time IT decides to update our JVMs. Does anyone know of a way to do this?
It's always a good practice to decouple the execution of your application from the configuration of the machine it is running on. In your case the first task will be identifying where you want to store the extra libraries and configuration files that are needed (its probably best to bundle them with your application). Once that is done, then you can configure your application to find them at launch:
Assuming the following directory tree:
myapp
|
--lib (archives and shared libraries stored here)
|
--resources (configuration files go here)
You could do:
java -Xbootclasspath/a:myapp/lib/comm.jar -cp "myapp/lib/*:myapp/resources" -Djava.library.path="myapp/lib"
Your JAR and DLL files would go into the lib sub-folder, and the property file would go into the resources subfolder.