Tuesday, 14 December 2010

ADF: changing your application's URL (or what on earth is a context-root anyway?)

When you start running ADF applications under JDev 11g you'll become familiar with seeing URLs like the following in your browser:

Each part of the URL is significant, though the one that stands out as being odd is the rather cumbersome "Sage-ViewController-context-root". This part of the URL in Java EE parlance is called the context-root, and not just because our example literally has context-root written into it.

The context-root at its most basic form is the unique part of the URL on the application server to clearly distinguish your application from another on the server. However as you can see in this example the "Sage-ViewController-context-root" is hardly something you'd want to present to users. Something more palatable like "Sage" or "HR" would be useful, delivering an end URL like:

So how do we go about making a friendly context-root for our applications? Essentially there's 2 locations.

Design time

If you're running your application from JDeveloper by opening & running a jspx page via the right mouse menu or similar, when the application is deployed to the integrated WLS server, it will use Java EE Application options under the ViewController's Project Properties, accessible by right clicking the same name option of the ViewController project:

As you can see in the above picture, the Java EE Web Context Root is set to "Sage-ViewController-context-root". Simply change this to something desired.

Deploy time

Once you've finished testing and running your application in your local integrated WLS instance, you're likely to generate an EAR or WAR file to deploy to an external WLS server. In this case the context-root is influenced by settings in the WAR file for the ViewController project. Simply right click the ViewController project -> select Project Properties -> Deployment and locate the associated WAR file deployment profile. In the following picture you can see it's called "Sage_ViewController_webapp1":

If you then select the edit button for the WAR deployment profile, the first node General includes 2 options, namely Use Project's Java EE Web Context Root and Specify Java EE Web Context Root. If you select the 2nd option and enter your own option, this will override the context-root once the application is deployed to an external WLS server:


Susan said...

I have an issue related to ectx.getRequestContextPath()
I am developing an application in Jdeveloper

While excecuting ectx.getRequestContextPath() the result I get it is

But for me to navigate to the next page I only need
ie i dont need the xyz part

My exact statement is like this
String url = ectx.getRequestContextPath() + "/faces/Login.jspx";

and the result I am getting is

instead I need


Chris Muir said...

How about just substringing the result to the location of the the 3rd or 4th slash?


Java & ADF said...

you can even make the url more pretty by deploying the application to a virtual host and set the managed server port to 80 and the context root to only "/"
you url will looks like :