9-Sep-04 (Created: 9-Sep-04) | More in 'Howto'

How to use AspireContext for writing portable web applications

What is AspireContext?

When you write web applications they usually reside in a root directory. This root directory can be anywhere and on any hard drive or any path. J2EE calls these applications "web apps". J2EE servers know how to run these web apps once the root directory is registered with them.

For example if your directory is


c:\myapplication

You can register it with a J2EE app server as


myapp -> c:\myapplication

This word "myapp" is called a context. This context allows you to access your application as follows


http://your-host/myapp/index.html

This value "myapp" is available for use with in Aspire as a request level and also application level variable called "AspireContext".

Why use AspireContext

While coding web pages in a web application, on more than one occasion you may have to use this web app context to address files or resources inside the web application. If this is hard coded then you can not register the application under a different name. A relative URL will not work because web servers like Apache and IIS are unaware of the web application context.

How to use AsprieContext in your web pages


<script>
function gotoPage1()
{
	url = "/{{aspireContext}}/my-sub-dir/pag1.html";
	document.location = url;
}
</script>

Assuming your context name is "myapp" The above code when expanded by Aspire will become


<script>
function gotoPage1()
{
	url = "/myapp/my-sub-dir/pag1.html";
	document.location = url;
}
</script>

Differences between Web application context and AspireContext

J2EE servers will use a leading '/' for a web application context. For example it will be "/myapp". In Aspire the forward "/" is stripped off.

How to use AspireContext while redirecting from server side

Consider the following web request in aspire. This request logs in a user if the user is valid. And if not the user is redirected to the login page again with the view state intact and an error message.


#############################################
#LoginRequest(username, password, viewstate)
#############################################
request.LoginRequest.classname=com.ai.parts.AspireLoginPart

request.LoginRequest.redirectURL=\
/{aspireContext}/display?url=welcomePageURL&username={username}

request.LoginRequest.failureRedirectURL.INVALID_PASSWORD=\
/{aspireContext}/display?url=loginPageURL&pageStatus=invalid-login&vs={viewState}

Notice how aspireContext is used in the redirections. In the future releases I might make these server side redirections relative to the webapp. But for now you can use the AspireContext key word. Whereever possible this is case insensitive.

Build information

This functionality is available from build 20 on wards. Some times depending on app server you may not have the web context available. In that case you can use property file entry like


aspireContext=myapp

in any of your properties files.

Affected Aspire Classes/Source files

  • ServletUtils (main change for this support)
  • AppInitServlet1
  • BaseServlet (indirectly)

Further references

1. Build 20 release notes