context.xml vs web.xml in web application - java

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.

Related

Is it possible to deploy multiple war files under the same deployment path?

Let's say I have:
foo.war
bar.war
Is it possible that I deploy them both somehow to the same deployment path? E.g., to access it at:
http://localhost/baz
Are the content of the war files merged somehow? How are file conflicts handled (e.g., let's say both of them has an index.jsp file)?
Thx in advnace!
The servlet specification explicitly forbids this. Deployed web applications may not have identical or overlapping context roots. From the Servlet 3.0 specification, section 10.5:
Since the context path of an application determines the URL namespace of the contents of the Web application, Web containers must reject Web applications defining a context path that could cause potential conflicts in this URL namespace. This may occur, for example, by attempting to deploy a second Web application with the same context path.
Yes & no.
I don't think it's possible to somehow merge them into the same file system path within a servlet container like Tomcat (unless you were to write some kind of complicated, intelligent script to do so). For starters, each .war will have a WEB-INF/web.xml file, and each will rely on the contents of its own file to function -- which would win?
But you conceivably could...
Deploy to 2 different contexts (or containers, or hosts), and employ some kind of load balancer (hardware or software) to route some requests to one, other requests to the other.
Use an "overlay" strategy (such as Maven Overlays) to make a second (and final) .war that is a derivative and extension of another .war file

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.

functionality of tomcat servlet container

ServletContainer reads the user defined servlet class name from web.xml file by converting web.xml file into DOM object. I don't understand how servlet container converts this and where this DOM object (web.xml data) resides inside web-app directory of server?
The Java EE specification mandates a specific directory and packaging structure (war) for web applications so that the web app can be deployed on any servlet container (Tomcat is one of them) without any modifications. Now, each servlet container can unpack it in which ever way it wants and as a developer you no need to worry about it.
Now, Tomcat places all the deployed applications in the \tomact-install-dir\webapps directory. Each web app will be in its own folder with webapp name as thefolder name.
Perhaps this is the first place to take a look at when deploying the first web application. tomcat deployment hierarchy.
web.xml should be placed inside the WEB-INF on your webapp deployment. Some servlet containers allow you to have generic or reusable web.xml files inside their configuration directories but that's not standard AFAIK (It's better for your webapp to be selfcontained most of times)
You shouldn't need to know about the parsing of the XML file since it's all handled by the servlet container under the hood. (Just place it in the right place)

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?

Multiple contexts pointing to a single webapp

Is there anyway of configuring tomcat to point more than one context at a webapp?
I need to point these two urls:
http://server.com/abc
http://server.com/def
to a webapp running under the context abc.
Tomcat does not allow direct configuring of multiple <Context> elements to point to the same path.
So your options are either deploying the same web app twice with different Context (Not great idea)
or create a webapp called def that has one custom servlet filter declared in the web.xml that re-writes all requests to abc.
If your requirement is for a production app, I would recommend having an Apache Web Server before the tomcat so that you can do this and more.

Categories