How to access request header attributes using JSTL

To access request header using jstl you will have to include the funcations taglib as below (assuming jstl jars are already in the project classpath)

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

The syntax is as below

${header['user-agent']}

So in your JSP code you can do something like this

<c:choose>
    <c:when test="${fn:contains(header['user-agent'], 'MSIE')}">
        You're using Internet Explorer.
    </c:when>
    <c:otherwise>
        You are using some browser other than IE.
    </c:otherwise>
</c:choose>

The jstl code inside test=”…” checks if your ‘user-agent’ is Internet Explorer and shows message accordingly.

Caution: Please do not mix scriplets and jstl tags in jsp. They do not run in same scope and hence tend to cause problems. The golden rule is “AVOID SCRIPLETS IN JSP”.

Advertisements

, , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: