Over a million developers have joined DZone.

How to get the assets like images on the server behind the firewall?

To retrieve the asset in the web server behind the firewall, the getLocalName() and getLocalPort() methods must be used to reconstruct the URL.

The request.getServerName() method returns the name of the server that the HTTP request was sent to.  If this method is used, it would return the name of the public server name configured in the firewall.

The request.getLocalName() method returns the name of the server that actually received the request. So this must be used to reconstruct the URL and get the desired asset in the web server.

Note: Asset could be a template (XSLT), image etc.

public static String getAssetUrl(HttpServletRequest request, String fileName) {
    String scheme = request.getScheme();               
    String localServerName= request.getLocalName();
    int localServerPort = request.getLocalPort();
    String contextPath = request.getContextPath();
    String pathInfo = request.getPathInfo();      

    // Reconstruct original request URL
    String url = scheme + "://" + localServerName+":" + localServerPort + contextPath + "/asset?fileImage=/" + fileName;

    if (pathInfo != null) {
        url += pathInfo;

    return url;

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}