Find context path from Tomcat's server.xml with java - java

Today I have a problem reading the server.xml config file from Tomcat. What I mean by that is I want to verify the app context on server start so the app will know which configuration file to look for.
For example: I have in my Tomcat server configuration set a context for my app "/myapp". So it will be accessible by the :8080/myapp.
No I want my Java application to read the server.xml file on Tomcat startup, so it will look more-or-less like this:
- Tomcat is starting,
- App is checking the server.xml file for itself context parameter, and finds that it's "/myapp",
- Using the above context - the app looks for file /CATALINA_BASE/conf/myapp.conf.
I can't base on the URI because the app starting with Tomcat and doesn't require to open the web browser, so no URI is provided on startup.
Thank you in advance!
Tom

You shouldn't parse server.xml, as it's not guaranteed that your app is declared there (in fact, it's bad style to do so). The application's context path can be determined within a ServletContextListener from ServletContext.getContextPath().

Related

Tomcat Deployment and Routing

I feel like I’m attempting to accomplish a pretty simple task but I’m stumped. I’m attempting to use the Tomcat Manage App to deploy my app and then route my root domain name to point to that deployment.
Specifically, the app currently deployed at http://www.schmud.de/home/ should load when a person types http://www.schmud.de into their browser.
I tried deploying and configuring this in CPanel with no luck. It seems that the Tomcat Manage App interface or Host Manager interface is what I’m supposed to be use to configure my routing?
My .htaccess currently looks like this:
SetHandler jakarta-servlet
SetEnv JK_WORKER_NAME ajp13
My web.xml is generated by Clojure.
There is no way to do it within the Tomcat Web Interface nor the .htaccess file. The user must have deeper access on a CPanel system.
Assuming that you have deployed your application as "app" and configured your .htaccess as I described above, here are the specific instructions I gave my root admin to solve the routing issue:
open /usr/local/easy/share/easy-tomcat7/conf/server.xml
Under the <Host> tag add this:
<Context path="" docBase=“app” debug="0" reloadable="true" />
Restart the server
Upon restart, http://domain.com/ should load my homepage (just as http://domain.com/app/ does now)

Static deployment on Jetty

How can I configure jetty to deploy only one specific web application, and disable hot deployment (disable monitoring webapps directory, disable checking this single application for changes) ?
I would use the context provider only, and just disable the webapp provider. Then you have no scanning of the webapps directory, just a context file that points to your webapp.
edit the start.ini file and make sure you have the jetty-deploy.xml and jetty-contexts.xml files active, and comment out the jetty-webapps.xml line. Then you just need a file in the contexts directory that points to your webapp.
more on the context provider here:
http://wiki.eclipse.org/Jetty/Feature/ContextDeployer
If i get your question right then it means you want to to embed jetty in your application as compared to deploy your web application in jetty.
If that is the case the read this: http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty else tell me exactly what you are stuck with.

Opening files in the browser with Java Web App

I have a standalone simple java web application with servlets and jsp, say the name is FileDisplay I am access its home page through url - http://localhost:8080/FileDisplay/index.jsp.
What the application essentially does is, retrieves a list of file names(.xml's and .pdf's) with complete path. These files are stored in various external directories, say D:\ABCD, D:\XYZ, D:\PP\2012\08 etc but on the same machine as the web application just on a different drive. So the return list is something like-
D:\ABCD\filename1.xml
D:\ABCD\filename2.xml
D:\ABCD\pdf1.pdf
If I use a simple <a href=""> in the jsp then it doesnt work. in the viewsource it looks like -
file1
I think it is beacause these files are not part of the webapp, so the container doesnt think it is local and hence unable to open them. When I place the mouse pointer over the link, the status bar shows as file:///D:\ABCD\pdf1.pdf. I also tried prefixing file:/// in the href, even then it doesnt work. So I tried a few other things.
One thing I tried is setting the Context in Tomcat's server.xml but even that doesn't seem to work. I am using eclipse to build and deploy and run the tomcat, so the server.xml I modified for this context is one within the eclipse workspace.
The setting I used is -
<Context docBase="D:/ABCD" path="/File-Display/NB" reloadable="true"/>
I have another context set for the main application which is -
<Context docBase="FileDisplay" path="/FileDisplay" reloadable="true" source="org.eclipse.jst.j2ee.server:FileDisplay"/>
What am I doing wrong here?
Does it explain a bit more now?
I think you are on the wrong way.
If you want to provide access to different files distributed in your file system create controller (servlet) that accepts URL, reads appropriate file and writes it to the response output stream.
This way you can control access to your resources, make them secure, etc. You will be able to modify your application easily (e.g. if you change the files location). Etc, etc.

context.xml vs web.xml in web application

I am developing a small web application application. The objective is to create one welcome index.html page with Ajax + one servlet to handle ajax requests.
Although I thought I would be fine with a web.xml only, I don't want to deploy to /, but to /MyApp. NetBeans's project properties offers options to set a context path, which helps me deploying to /MyApp. However, it automatically adds a /META-INF/context.xml file, which is a bit confusing.
My questions are:
1) Do I really need a context.xml file do deploy to /MyApp instead of /?
2) If answer to 1) is no, how to accomplish the same with web.xml only?
3) What is exactly context.xml to web.xml?
/META-INF/context.xml is a Tomcat-specific config file. It's used to configure how your app is deployed to Tomcat, including, among other things, the context path at which it exists. Other containers have similar files that can be included in a WAR for container configuration. To answer your questions:
No. The embedded context.xml is only one way to set the context path, and as I indicated, it'll only work in Tomcat. In Tomcat, default behavior is to deploy webapps to a context that has the name of the war file, without the ".war" extension.
You can't set a context path in web.xml. That's your application's deployment descriptor. It configures your application, and the context path is external to your app. It belongs to the server/container you're deploying the app to. Configuring a context path is always done in the container's configuration.
If by "config.xml", you meant "context.xml", then I think I've already answered that. If not, clarify your question.

Short url or alias for deployed application in tomcat 6

I have a web application project which is deployed in tomcat 6.
I can access my application using the url:
http://localhost:8082/MyApplication
I also wan't to be able to access this application by another url like:
http://localhost:8082/myapp
Is this possible ? if yes what alternatives do i have ?
Off course, I don't want to change the original name of the application('MyApplication').
Thanks,
Abhishek.
If you add the Context within server.xml it will work as you want. Give the path attribute you wish.
<Context docBase="MyApplication" path="/myapp" />
Though it works, this approach is not recommended by the Tomcat docs, since any changes to server.xml means restarting the server disturbing all the web apps.
But, on the flip side, the practice of keeping this in Catalina_Home/conf/Catalina/localhost/context.xml (which is recommended by the docs) has some unreliabilities as others have reported - when you redeploy the war you can lose the context.xml too
See Why-does-tomcat-replace-context-xml-on-redeploy and
Why does tomcat like deleting my context.xml file?

Categories