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:

<?xml version="1.0" encoding="UTF-8"?>
<application version="6" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd">
  <display-name>JustMyApp</display-name>
  <module>
    <ejb>JustMyApp-ejb.jar</ejb>
  </module>
  <module>
    <web>
      <web-uri>JustMyApp-war.war</web-uri>
      <context-root>/JustMyApp</context-root>
    </web>
  </module>
</application>

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 !

Enjoy.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: