Skip to main content

Calculating Domain URL in Java Web Applications

When you are developing web applications, its obvious that you’ll write code to generate paths for resources like images, stylesheets, scripts, etc. You can do this by simply using relative paths. In your JSP application, you can do this using ‘getContextPath()’ method of ‘HttpServletRequest’ object.
Lets see what this method gives you. It returns a String value, the returned string will always have a leading ‘/’ character and will never have a trailing ‘/’. This method returns an empty string when you deploy your application as ROOT context and returns the subdomain context when deployed as subdomain.
For example, if you name your WAR file as ‘myExample.war’ and deploy it in TOMCAT [by placing the WAR file in ‘webapps’ directory]. The ‘getContextPath()’ method will return ‘/myExample’.
In your JSP page, if you’ve a image tag pointing to an image resource and you want it to work even when deployed as subdomain you can do that by using this ‘getContextPath()’ method as used below.
<img src=”<%=request.getContextPath()%>/images/sample.jpeg” alt=”Sample Image” />
There will be situations where you want to use complete path instead of relative path, and you still want the code to work in all the cases when deployed as ROOT and subdomain in your development environment and even in production sever without any changes. The following code will do that exactly for you. It’ll calculate the complete URL dynamically using the servlet API.
public static String getBaseUrl(HttpServletRequest request) {
    StringBuilder sb = new StringBuilder(27);
    int port = request.getServerPort();
    if (port != 80) {         sb.append(':').append(port);     }
    sb.append(request.getContextPath()).append('/');     return sb.toString(); }
Its really simple code, the above method takes the request object [HttpServletRequest instance] as parameter and uses it to calculate the URL. First we’ll append the protocol to the URL string then use the ‘getServerName()’ method, to get the server host name. For example on production server you might get this value as ‘’ and in development environment you might get this value as ‘localhost’. After this we’ll use ‘getServerPort()’ method to get the port number to which the request was sent. We’ll compare this port number and see whether its not the default port ‘80’. If it is not then append the port number to the URL. Then call the ‘getContextPath()’ method to get the subdomain value. Finally append a trailing ‘/’ to the URL before returning the calculated URL.
That’s all for now!


Popular posts from this blog

Hosting Multiple Domains In Tomcat

Tomcat allows us to host multiple domains in one instance, using multiple 'Host' tags. In this article I will explain how to do it on Tomcat. This is very simple configuration using 'Host' tags in your server.xml. A novice can also understand this configuration very easily.
Before going into the details of the configuration first lets have a look at the 'Host' tag, 'Context' tag and 'Alias' tags first.
<Host name="domain1" appBase="[application base]" autoDeploy="[true/false]" unpackWARs="[true/false]"> <Alias>...</Alias> <Context path="" docBase="" reloadable="[true/false]"/> </Host> First lets have a look at 'Alias' tag. This tag is used to provide aliases for your actual domain. For example you have a domain called '', and you want to run the same application for '' also…

File Uploading Using Servlets, JSP and Commons File Upload API

I’ve seen many developers who are at the early stages of their career have problems with this topic and seen many posts in forums asking how to do it – File Uploading using Servlets, JSP!; this article will provide an example using Commons File Upload API. I tried to make the example as simple as possible, hope it helps those early birds. Example uses JSP to provide the pages with form where user can select the file to upload with other form fields, Commons File Upload API to process submitted form and read form fields separately, and Servlets as middle layer between JSP and Commons File Upload API, example also has ANT build script to generate the distributables. All the code can be downloaded, links to these resources are provided at the end of this post, lets get on with the example then.
The flow in this example is as depicted in the following picture.
As you can see, user selects the file to upload and provides normal form data as well, using "index.jsp" and submits th…

About Rakesh.A

As the URL and other parts of this blog says, I am Rakesh.A, a techie form Hyderabad, India. I am working for coMakeIT, Hyderabad as a Sr. Software Engineer, that's about me in short.
How I ended up in this profession? - Well when I was doing my schooling I never thought I'll be into this profession, infact I wasn't that interested in Information Technology/Software Development, so never concentrated on it even after my parents stressed to get into it. After couple of years, I saw myself doing the same. And now I can't even think of myself being in a different profession. Anything I can do with whole heart, that's only PROGRAMMING/DEVELOPMENT.
As every student who got into this profession, I felt my FIRST LOVE with 'C language', and I love coding in C. Then I started learning Object Oriented Programming and got attracted to 'C++ language', but it was 'JAVA' in which I saw my soul mate. That's it I got married to it and settled. I've…