开发者

How to distinguish between domains in JSP using JSTL

I wanna distinguish between the requestUrl inside of a JSP and render different Content depending on the URL.

To be concret:

<c:set var开发者_开发知识库="url" value="${pageContext.request.requestURL}" />
<c:choose>
  <c:when test="${fn:contains(url, 'google.com')}">
    1 - Google.com
  </c:when>
  <c:otherwise>
    2 - Google.co.uk
  </c:otherwise>
</c:choose>

In this case every time option 2 will be rendered. I really don't know why.

How can I do this the right way? Or is there a better way to do this in JSTL?

Cheers everyone


Try changing the first line to this:

<c:set var="url" value="${pageContext.request.requestURI}" />

EDIT: I forgot there's a getServerName() method also which should give you the requested host without the rest of the URL. That's probably the better method to use.

getRequestURI() on HttpServletRequest returns a String while getRequestURL() returns a StringBuffer which doesn't have a contains method.

NOTE: I have not tested this code so ymmv.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜