IntelliJ (artifact) context root and servlet-mapping

Hi there,

I have some issues regarding Vaadin projects in combination with IntelliJ.

I have 3 modules and 2 of them contains an artifact.
The “servlet-mapping” is already set with different paths:


web.xml (module 1)


<servlet-mapping>
		<servlet-name>Demo Application 1</servlet-name>
		<url-pattern>/demo1/*</url-pattern>
	</servlet-mapping>


web.xml (module 2)


<servlet-mapping>
		<servlet-name>Demo Application 2</servlet-name>
		<url-pattern>/demo2/*</url-pattern>
	</servlet-mapping>

When posessing two artifacts, both context roots (in the “run configuration”) can’t be equal. If I set them to “demo1” and “demo2”, I have to type the url pattern twice in the browser in order to find them:


http://localhost:8080/demo1/demo1

This workaround is not very nice. Changing the “url-pattern” to ROOT (/*) will hold the application from finding its javascript files or will end up with an MIME type Error:



Resource interpreted as Script but transferred with MIME type text/html

I would really appreciate a solution, which let’s me have multiple amounts of artifacts and will not need to type the url pattern twice in the browser, since I’m limited with not allowing more than one artifact with the context root as ROOT (/).

I’ve also experienced that actually all files are referencing to the web.xml, but as told before… that ugly rule in IntelliJ (run configuration | Deployment tab) is just holding me off.

I probably should not be replying, as I am certainly no expert on this. But here goes…

I suspect you are ignoring the concept of the “Context”. Your servlet operates within part of the URL, the “context path”.


http://tomcat.apache.org/tomcat-7.0-doc/config/context.html

By default, if you create a Vaadin app, export it as a WAR file, and then move it to a web server like Tomcat, the WAR gets automatically deployed which includes re-hydrating or expanding the WAR file back to a folder. That WAR file name (and folder name) defines the context, by default in Tomcat.

For example, I create a Vaadin app called “AcmeInvoices”. I export a “AcmeInvoices.war” file. I drop that into Tomcat’s “webapps” folder (or in my case, a folder for the “virtual host” web site). The war file gets expanded back to “AcmeInvoices” folder. By the way, when deploying a fresh WAR, you must delete the existing expanded folder too.

If my domain is “acme.com”, then the URL for my app would be:

http://www.Acme.com/AcmeInvoices/

Note that a domain name is case-insensitive. So you can choose to use any variation such as http://www.acme.com/ or http://www.Acme.com/
But the rest of the URL is case-sensitive. So it must be "
/AcmeInvoices/
" to match the WAR file/folder name.

The "

" tag you show in your posting here refers to what appears
after
this first part.

So

refers to the “dog” and “cat” portion of these two URLs:

http://www.Acme.com/AcmeInvoices/dog
http://www.Acme.com/AcmeInvoices/cat

In basic Vaadin apps, we don’t use such URLs with the “dog” or “cat” portions. Therefore, the usual Vaadin servlet-mapping’s url-pattern is:

/*

That says we want URLs with anything (or nothing) after the “/AcmeInvoices” to be handled by our vaadin app.

I let the Eclipse plugin generate my Vaadin app’s “web.xml” file. In there I find this servlet-mapping:

<servlet-mapping>
  	<servlet-name>Acmeinvoices Application</servlet-name>
  	<url-pattern>/*</url-pattern>
</servlet-mapping>

The ‘i’ of Invoices is lowercase because of the Eclipse Vaadin plugin’s naming conventions. But the important point in this example is the "

" tag.

So why does the url-pattern refer to just the portion after the context name, and not the entire URL? Because the first part of the URL is none of the Servlet programmer’s business. The webmaster defines the URL that makes up the “context” in which your servlet operates. The webmaster can define and change the URL mapping to your servlet without consulting you, and your servlet should keep on working. If your Vaadin app cares about the URL, any such URL should never be hard-coded as you cannot assume what the URL will be at deployment. Defining the context’s URL is not part of the Servlet spec (asfaik) – the context’s URL is up to the implementation of the servlet container (the web server) and the webmaster.

I learned the above info while trying to discover how to change the URL. Instead of my app responding to this URL:

http://www.Acme.com/AcmeInvoices/

I want to use this url:

http://www.Acme.com/invoices/

I don’t yet know the answer, but I believe

in

is the wrong way to go. In the case of Tomcat, I suspect the answer is the

tag (note the uppercase ‘C’). The confusing part is that there are many ways & places & defaults for

.
[Update]
Seems the easy answer is simply rename the WAR file. See
my other Forum thread
.

I hope this helps. But remember my caveat at the top: I may not know what I’m talking about here.

–Basil Bourque

I’m not sure I completely understand your issue, but are you creating two war files, demo1.war and demo2.war? If so, you can set the servlet mapping in web.xml in both of them to simply /* and deploy them both. The first app will be available at http://localhost:8080/demo1 and the other at http://localhost:8080/demo2. You should probably figure out why your /* mapping is causing errors, as I use it all the time without an issue.

If you really need to map your applications to something like /demoX/*, then there’s no way around having long-ish URLs. An application is deployed to a context (which can be /), and a servlet within that application is mapped to some url pattern. So a servlet lives at:

http(s)://[:port]
//

This allows you to have more than one application deployed to the same server (based on context root), and more than one servlet in the same application (based on url mapping).

Cheers,
Bobby