context.xml vs web.xml in web application

2020-02-26 12:13发布

问题:

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?

回答1:

/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:

  1. 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.
  2. 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.
  3. If by "config.xml", you meant "context.xml", then I think I've already answered that. If not, clarify your question.