I have one folder which has some PDF files which is used by my application, but when I include this folder in my war file , the size of war file increase to GBzz , so I thought of keeping this folder in some server location , build and deploy the war without the folder which will have the relative path of this folder , can any one help me on this? I am not sure how to do configuration for this to read the folder ?I am using tomcat server.
The easiest way starting from Tomcat 7 is to configure alias paths i.e. paths on the disk that will be aliased in Tomcat. Check this section in the guide for aliases attribute.
You want something like this in your server.xml:
<Context docBase="AppName" path="/appname" aliases="/pathPdf=c:\pdfs"/>
In case you're on an older Tomcat, your best bet is to setup a servlet that will serve files from the desired external location.
Related
I've installed Eclipse with web development and during download have installed Tomcat7 to dir E:\Eclipse\tomcat7.
When in Eclipse and trying to test the program I'm getting a realPath of
C:/Users/user/Documents/eclipseJEEWorkspace/javaWebPages/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/org.example.web/WEB-INF/classes/
as my real path and not what I expect which is E:\Eclipse\tomcat7\webapps\org.example.web/WEB-INF/classes/
1) how do I get the real path of the servlet rather than the temporary path...
or am I missing something with how Eclipse uses the installed server? If this is the way then do I have to continuously create the WAR and fiddle around recreating/deleting all the time?
The reason for this is I'm also trying to get a JAVA Servlet getting access to a file in a path higher than webapps. I'm new to servlets....
My development includes third party software that all reference an individual file our.properties so changing the structure is unfortunately not a option.
My directory structure is:
/tomcat
/mycompany
/properties
our.properties //the file we want to access
/*otherfiles
/html
/*not used in this context but to show usage
/javascript
/*not used in this context but to show usage
/webapps
/org.example01.web
/META-INF
/WEB-INF
/org.example02.web
/META-INF
/WEB-INF
/org.example03.web
/META-INF
/WEB-INF
How do I tell my Servlet to access the our.properties file?
I've tried getPath, getResource but without getting the first bit to work, I doubt I'll be getting anywhere fast.
Thanks
When you run Tomcat under Eclipse, via the Tomcat plugin, the webapps path is changed. Eclipse uses .metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ folder to deploy the project. That's not a temporary path, it's the actual path since your web application is deployed there.
If you run tomcat from command line, or as a service and deploy your web application, you'll see that the path of the servlet will be as you expected.
In order the change the default path to deploy for Eclipse, double click to your Tomcat under Servers view and modify Server locations section.
This may not be a difficult question, but I probably do not have the right terminology and am not finding the answer.
When I deploy a war file in Tomcat (7), say, myapp-1.0.war, a new directory, myapp-1.0, is created under webapps. Is there a way to change the name of the new directory? e.g. When I deploy myapp-1.0.war, the new directory is named myapp? Is this configurable inside the war file somehow?
I have implemented this by adding a symlink to the webapps directory and placing my war file in a directory outside of the appBase. This way I was able to standardize the docBase in the server.xml. Now all I have to do is update the symlink when new versions are posted (and maybe restart the server). My configuration is posted at another question:
Tomcat 7 symbolic link to war file
I previously used the following to get my context path on Tomcat-5.0.28 and earlier:
String context_path = context.getRealPath("/WEB-INF/");
This worked to return the path to that folder.
But on OpenShift (Tomcat 6 - JBoss EWS 1.0) this returns
/var/lib/openshift/53.*context_id_here*..18/jbossews/null
The null should be:
work/Catalina/localhost/_/WEB-INF
How can I get the path to the WEB-INF folder on OpenShift using JBOSS/Tomcat?
A little background information: When I ran this struts webapp on my own Tomcat server, I deployed a appname.war file in the webapps directory and waited for it to expand (since I had set that option in the server.xml file). Then I move a folder to webapps/appname/ folder with xml files I need to read and write to for my app to work. On OpenShift I used jar xvf appname.war to extract the war file by hand (because that's the default and I don't know how to change it), and then moved the files folder (from the same directory as the war file in my folder after a git add and push) to work/Catalina/localhost/_/WEB-INF/
This is causing a NullPointerException for me when trying to use that path as shown above.
I think you should be using something like getServletContext or getRealPath, and reading it from the web root instead of trying to find the file on the physical disk. That way your war file can run anywhere without issue. Try looking up both of those and see if one fits your use case.
I'm trying to deploy a war on Tomcat 7.0, and in the server.xml file, I've set unpackWARs = true and autoDeploy = true.
Since I want this war file to be the default web application, I have deleted the ROOT folder inside ...Tomcat7.0/webapps. Then on, when I try to start the Tomcat service, I see a behavior that I have yet failed to identify a pattern in:
Sometimes, the war is unpacked to a newly created folder named
'ROOT'.
Some other times, it is unpacked to a folder that is named
the same as the name of the war file. E.g. if the war file is
'MyWebApp.war', it's unpacked to a folder named 'MyWebApp'.
It seems to be completely erratic and random, but I am sure there is method behind this apparent madness. As a rule, I am not sure if there is anything that dictates the creation of a ROOT folder. I have tried to edit the server.xml file and add context to my web application in it, but it doesn't seem to make a difference.
What am I missing? Thanks in advance!
Name your war file ROOT.war. It will unpack to ../webapps/ROOT and map to / context
I have this folder under Tomcat webapps/mysite which is where all my JSPs and other things are located. To access this folder I go to http://blah.com/mysite and it works just fine. However (because of stylesheets and images statically connected to the root /) I have to make it so that when I go to http://blah.com/ it will load the stuff inside webapps/mysite.
I've tried many different things including contexts and setting the absolute path in server.xml... nothing seems to work, whenever I go to http://blah.com/ it still tries to load the ROOT folder... what's happening here?
The solution I use is to set this in your Tomcat server.xml
Add a <Context> element within the <Host> like below which sets your mysite as the default web app. Note the empty path="" which makes it the default.
<Context docBase="mysite" path="" />
Attributes of Context Container from the Tomcat docs:
docBase You may specify an absolute pathname for this directory or WAR file, or a pathname that is relative to the appBase directory
of the owning Host.
path All of the context paths within a particular Host must be unique. If you specify a context path of an empty string (""), you are
defining the default web application for this Host, which will process
all requests not assigned to other Contexts.
See others who have had similar question and the similar answer here, here and here
See also Apache Tomcat Configuration Reference - Context
There are a number of ways to make an application the root application. The simplest way is to just replace the contents of webapps/ROOT with the contents of your web application.
For other solutions, please see the following website:
http://wiki.apache.org/tomcat/HowTo#How_do_I_make_my_web_application_be_the_Tomcat_default_application_.3F
https://stackoverflow.com/users/1123501/george-siggouroglou 's awnser works but lacks a step.
delete ROOT and all items
copy the war to webapps as ROOT.war
Without the deletion, it may not work. Tested with docker.
You can rename your war from something.war == to ==> ROOT.war.
So, tomcat will unpack the war and will create the folder ROOT for it.
It is a trick that is working on tomcat 8 also.