Tomcat context docbase not working to serve static images - java

I need to serve images directly from a folder outside war file using tomcat server.
I tried few options like setting the image location path in context docbase but its not working.
I configured that in server.xml of tomcat as follows
<Context docBase="E:\images\" path="/images" />
I need a solution directly from tomcat side and cant make any code changes to serve them.
Already gone through these links but of no use:
How to config Tomcat to serve images from an external folder outside webapps?
Tomcat version : 8.5, my images are in .svg format.
Please help me in this.

This may help #Kirti or someone who is facing this problem.
I tried with following solution in same case in my project:-
I created a directory on server as E:\MyProject\Images
Added following <Context /> tag in server.xml inside <Host></Host> tag
<Context docBase="E:\\MyProject\\Images" path="/images"/>
Then I could simply access the images using following link:
http://localhost:8080/images/firstImage.jpg
Note: For windows we separate path with \\ and in linux need to use //

You are missing the Closing slash of the context tag. Also forward slashes are preferable and work as well on Windows.
<Context docBase="E:/images/" path="/images" />

Related

Tomcat custom context.xml file is never taken into account

I have currently a problem with the Tomcat configuration of a web application (I use Tomcat 8.5.57).
This web application is packaged in a war file containing, among others, html files and css files.
This application works well.
Now, I have a request from a customer asking to be able to modify the look and feel of the application from outside of the war via a custom css file managed by the client (used to set the logo of the client or stuff like that).
So I tried to create a custom context file, called custom.xml, that I placed in tomcat\conf\Catalina\localhost directory.
This file looks like :
<Context docBase="E:/somedirectory/support"
path="/app/css"
reloadable = "false"
unpackWAR = "false"
swallowOutput = "true" >
<WatchedResource>custom.css</WatchedResource>
</Context>
I put the custom.css file containing some css instructions as test in the E:/somedirectory/support directory.
In the html file of my web application, I have the following line in the head section :
<link rel="stylesheet" href="css/custom.css" media="screen" type="text/css"/>
The problem is that my custom.css file is never taken into account.
When I open the Sources tab of Chrome's developer tools, I see a custom.css file in the hierarchy in app/css as expected (probably due to the line in the html file), but it is hopelessly empty.
I tried a lot of things found on the Web and on stackoverflow, but nothing worked for me...
Can someone help me ?
Thank you !
The path attribute of <Context> element is ignored outside of server.xml:
This attribute must only be used when statically defining a Context in server.xml. In all other circumstances, the path will be inferred from the filenames used for either the .xml context file or the docBase. [from Tomcat documentation]
Therefore you have two choices:
You can define a new context (new web application) with context path /app/css by creating a file named conf\Catalina\localhost\app#css.xml and content:
<Context docBase="E:\somedirectory\support" />
This way everything under the /app/css subtree will only be served from the E:\somedirectory\support directory.
You can redefine your application context to include an additional virtual directory (beside the contents of the WAR file) by adding a file named conf\Catalina\localhost\app.xml with content:
<Context>
<Resources>
<PreResources className="org.apache.catalina.webresources.DirResourceSet"
base="E:\somedirectory\support"
webAppMount="/css" />
</Resources>
</Context>
This way, while serving a request for /app/css/foo/bar, Tomcat will first look for foo/bar in E:\somedirectory\support and then in the WAR file.

Provide <Realm/> info to Tomcat on eclipse

Our web application has an appName.xml file in our Tomcat directory structure at <tomcatInstall>/conf/Catalina/localhost/. Its entire text is:
<?xml version="1.0" encoding="UTF-8"?>
<Context crossContext="true">
<Realm className="com.blah.ApDataSourceRealm"
dataSourceName="jdbc/blahDev"
roleNameCol="blahRole"
userCredCol="blahToken"
userNameCol="blahCol"
userRoleTable="blahTable"
userTable="blahTable"
/>
</Context>
I made sure to delete the tomcat 8 install in eclipse and then add it back again; I understand that is what copies all the tomcat installation stuff to the workspace locations. When I attempt to run the server on eclipse, the login page is displayed, but the username/password is always rejected.
I fixed it once by finding the eclipse copy of server.xml in the eclipse-workspace folder tree (...eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/temp1/conf). But the next time I rebooted the computer and ran it again, the login failed, and on checking server.xml, I find that it was overwritten this morning. I assume eclipse overwrites it under some conditions, perhaps on eclipse startup? I put the <Realm ... /> tag into server.xml within <Context ... /> again, and it is working again.
Where should this information go? I'd like to put it somewhere that it won't disappear from eclipse. I don't think we want this realm tag in the actual server.xml (as opposed to the one local to eclipse), because this one is specific to the development environment. So where can it be put on my machine so that it is used by eclipse's Tomcat and yet doesn't interfere with the application's overall source?
One of the way is to put your <Context> configuration in the webapp/META-INF/context.xml.
Reference :
Individual Context elements may be explicitly defined:
In an individual file at /META-INF/context.xml inside the application
files. Optionally (based on the Host's copyXML attribute) this may be
copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to
application's base file name plus a ".xml" extension.
In individual
files (with a ".xml" extension) in the
$CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The context
path and version will be derived from the base name of the file (the
file name less the .xml extension). This file will always take
precedence over any context.xml file packaged in the web application's
META-INF directory.
Inside a Host element in the main conf/server.xml.
Default Context elements may be defined that apply to multiple web
applications. Configuration for an individual web application will
override anything configured in one of these defaults
Where should this information go? I'd like to put it somewhere that it
won't disappear from eclipse
So where can it be put on my machine so
that it is used by eclipse's Tomcat and yet doesn't interfere with the
application's overall source?
When you create a Tomcat in Eclipse , a server project will also be created. This project stores the configuration for the created tomcat instance which will be copied to Tomcat instance 's conf folder (i.e. eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/temp1/conf when tomcat starts.
So , if you do not want to add the Realm configuration to your project source codes likes /webapp/META-INF/context.xml , you can add it to the context.xml in the server project.

How to deploy a Ldap war file in tomcat? to access site localhost:8080

I have a problem. I created a web app and packaged it with "Ldap" name. Then I put it into tomcat webapps directory. When I enter localhost:8080/Ldap in a browser, it works fine. But I want to start this web app when writing localhost:8080. How can I do this in Tomcat. Is there any Tomcat configuration to supply this. Thanks
You need to provide a context.xml file with your webapp, containing a Context element with an empty attribute path. This is explained in the tomcat configuration reference.
<Context path="" ...>
...
</Context>
This context.xml file should be located under /META-INF/ in your web application (not /WEB-INF/).

Making a java web application as the root of a domain name in Tomcat 6

I'm currently developing a Java web application myapp and when deployed in Tomcat 6 server, I access myapp with this URL: http://localhost:8080/myapp
Instead I want to access my application using this URL: http://myapp:8080 since myapp will be the only application deployed in my Tomcat 6.
How do I do it?
This can be done in Tomcat in basically two ways:
Set path attribute of <Context> element in Webapp/META-INF/context.xml (or Tomcat/conf/server.xml, depending where you'd like to define it) to an empty String. E.g.
<Context path="">
Rename it to ROOT.war and Tomcat will automagically deploy it as ROOT.
Outside Tomcat there are more ways to do this, such as (virtual) proxy, URL rewriting with .htaccess, etcetera.
Im assuming you meen you want your url to be http://localhost:8080 not http://myapp:8080.
If you dont need the default apps that come with tomcat then just go to your webapps directory (where myapp is probably located) and look for another folder called ROOT (in my Tomcat 6). rename ROOT to something else and rename myapp to ROOT. This is a quick and messy way that works because the default host's appBase is webapps and the default app is ROOT.
If you had access to the tomcat/conf/ directory then you could edit the server.xml file but Im not very knowledgeable about server.xml so I wont try and walk you through it.

Deploying Web Apps as war Files

I'm having some trouble uploading and getting my web app on the net with my chosen host. I built a war file in Net Beans and asked my host to deploy it for me. This worked fine but to access it I had to point my browser to:
www.myDomain.co.uk/explodedWar
What of course I wanted was to be able to access it just by pointing my browser at:
www.myDomain.co.uk
The war file contains the whole app, index.html, images, classes etc.
Is this possible or am I missing something ? ?
You can call the war ROOT.war but the best way is to change the context path in the servlet container. To do this in Tomcat you would add the following to your server.xml:
<context path="" docBase="explodedWar" debug="0"/>
If you name your war ROOT.war (in Tomcat) it should do what you want.
This is a question you need to ask your host about since they are deploying it for you.

Categories