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.
That’s all for now!
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); sb.append("http://").append(request.getServerName());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 ‘www.xyz.com’ 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.
int port = request.getServerPort();if (port != 80) { sb.append(':').append(port); }
sb.append(request.getContextPath()).append('/'); return sb.toString(); }
That’s all for now!
Comments
Post a Comment