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:

http://127.0.0.1:7101/Sage-ViewController-context-root/faces/Welcome.jspx

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:

http://127.0.0.1:7101/Sage/faces/Welcome.jspx

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:

2 comments:

Susan said...

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

While excecuting ectx.getRequestContextPath() the result I get it is
http://127.0.0.1:7101/ABCD/xyz/

But for me to navigate to the next page I only need
http://127.0.0.1:7101/ABCD/
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
http://127.0.0.1:7101/ABCD/xyz/faces/Login.jspx

instead I need
http://127.0.0.1:7101/ABCD/faces/Login.jspx

Thanks
Susan

Chris Muir said...

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

CM.