Deal with Context-Path in Java EE

Maybe have you ever try to create an Enterprise Application in Java, containing both EJB and Web modules. And maybe have you noticed that the context-path of the web application is the same that the generated WAR file.
So let’s imagine you create an enterprise application named JustMyApp with two modules: JustMyApp-ejb and JustMyApp-war. After deploying your application, you just go to your website using an URL like this one (depending on which port your server is running): http://localhost:8080/JustMyApp-war. Well I have to admit I totally dislike to enter -war in the URL… Why not JustMyApp only?

Context-Path in Java EE is kind of strange. Indeed, if you only have a WAR (not en EAR) changing the CP for the WAR is working fine. But if you are in an EAR, things are little bit different!

So how to do that? You have to create a file called a Standard Deployment Descriptor and put it into your EAR. The name of this file must be application.xml. This file contains the name of all modules you have in the EAR, and you can specify the context-path for your Web module. Let’s see an example:

The URL to enter to access your web application is just specified by the <context-root>…</context-root> markup. So put there whatever you want and just try it ! That’s how the Context-Path for EAR can be changed !