1

I am currently sending the domain name to GA using:

_gaq.push(['_setDomainName', '${esapi:encodeForJavaScript(request.serverName)}']);

This outputs: www.somedomain.com

What is the most elegant solution in jsp to remove the www., leaving just somedomain.com?

Eran
  • 387,369
  • 54
  • 702
  • 768
Neil
  • 7,861
  • 4
  • 53
  • 74
  • Duplicate: [Get domain name from given url](http://stackoverflow.com/questions/9607903/get-domain-name-from-given-url). – skuntsel Apr 30 '13 at 16:06
  • I've voted to close, I believe my question is a duplicate – Neil May 01 '13 at 08:49
  • Mostly, yes, regarding extraction of value in a servlet, that's why I made the initial comment. Still, your question is **not a duplicate** in respect to how to do it using JSTL/EL. Check out my answer to find it out. – skuntsel May 01 '13 at 08:53

2 Answers2

3
    URI uri = new URI(url);
    String domain = uri.getHost();
    return domain.startsWith("www.") ? domain.substring(4) : domain;
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • By the way, `url` doesn't have public visibility. Moreover, in fact, there is no `url` proberty. The method you referred to is `HttpServletRequest#getRequestURL`. – skuntsel Apr 30 '13 at 16:12
  • Thanks skuntsel. I had intution of it but was not sure. Just wanted to give OP a headsup. Reverting back to url – M Sach Apr 30 '13 at 16:15
0

As the request implements HttpServletRequest you can access the URI directly by calling request.getRequestURI() to use in in conjunction with Get domain name from given url answer.

Also, be sure to check out javadocs on HttpServletRequest to find out other interesting methods like, for example, getServerName.


As to the problem of how you can get what you need in JSP, not in servlet/scriptlet, you'd need to use JSTL fn:replace function like this:

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

${fn:replace(request.serverName, 'www.','')}
Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • Be aware of reverse proxies like Nginx. Usually they don't touch the Server Name but they most definitely will change the originating IP and the protocol. You will have to get around this with special `X-headers`. – Adam Gent Apr 30 '13 at 19:38