Discussion:
Retrieving USER-AGENT Header
Nicole Luneburg
2007-01-04 23:14:07 UTC
Permalink
Hi,

I want to do this in my main.jsp page:

<%
if(request.getHeader("user-agent").startsWith("Mozilla")) {
%>

display this

<%
} else {
%>

display this

<%
}
%>

I don't know how to do the request bit ...
Eg is it:

HttpServletRequest request = new HttpServletRequest();
or is it just request.getHeader
or HttpRequest request = new HttpRequest();

Do i need libraries? If so, how does that work in magnolia?
Can I import stuff in magnolia jsp's like in a normal java project?

That's what I'm lost on!

Thanks

----------------------------------------------------------------
David Smith
2007-01-05 02:34:14 UTC
Permalink
You should be able to do this:

<c:set
var="mozUserAgent"><jsp:expression>((javax.servlet.http.HttpSerletRequest)pageContext.getRequest()).getHeader(
"user-agent" ).startsWith( "Mozilla" )</jsp:expression></c:set>

<c:if test="${mozUserAgent}">
<p>display....</p>
</c:if>
<c:if test="${not mozUserAgent}">
<p>display something different....</p>
</c:if>

There may be functions in the standard taglib to reduce this to
something like:

<c:if test="${not fn:startsWith( header.user-agent, 'Mozilla' )}">
<p>display....</p>
</c:if>

But I'd have to do more digging into the standard taglib docs than I
really want to right now. The first one should work without digging
into additional expression language functions.

--David
Post by Nicole Luneburg
Hi,
<%
if(request.getHeader("user-agent").startsWith("Mozilla")) {
%>
display this
<%
} else {
%>
display this
<%
}
%>
I don't know how to do the request bit ...
HttpServletRequest request = new HttpServletRequest();
or is it just request.getHeader
or HttpRequest request = new HttpRequest();
Do i need libraries? If so, how does that work in magnolia?
Can I import stuff in magnolia jsp's like in a normal java project?
That's what I'm lost on!
Thanks
----------------------------------------------------------------
for list details see
http://www.magnolia.info/en/magnolia/developer.html
----------------------------------------------------------------
----------------------------------------------------------------
Nicole Luneburg
2007-01-05 03:36:07 UTC
Permalink
Hi David,

Thank you very very much for that!
With a bit of tweaking, it now works!
Here's my code for anyone who wants to do anything similar:

<c:set var="mozillaUA">
<jsp:expression>

((javax.servlet.http.HttpServletRequest)pageContext.getRequest()).getHeader(
"user-agent" ).indexOf( "Firefox/" ) != -1
</jsp:expression>
</c:set>
<c:set var="ieUA">
<jsp:expression>

((javax.servlet.http.HttpServletRequest)pageContext.getRequest()).getHeader(
"user-agent" ).indexOf( "MSIE" ) != -1
</jsp:expression>
</c:set>

<c:if test="${mozillaUA}">
<p>You are using Mozilla! ...</p>
</c:if>
<c:if test="${ieUA}">
<p>You are using IE! ...</p>
</c:if>

Careful how to detect browsers ... I was doing this to detect Mozilla:
useragent.startsWith("Mozilla")
and it kept coming back as true for IE too, it's because its user agent
(at least for IE 6) starts with "Mozilla"!!!

Thanks again David!
Post by David Smith
<c:set
var="mozUserAgent"><jsp:expression>((javax.servlet.http.HttpSerletRequest)pageContext.getRequest()).getHeader(
"user-agent" ).startsWith( "Mozilla" )</jsp:expression></c:set>
<c:if test="${mozUserAgent}">
<p>display....</p>
</c:if>
<c:if test="${not mozUserAgent}">
<p>display something different....</p>
</c:if>
There may be functions in the standard taglib to reduce this to
<c:if test="${not fn:startsWith( header.user-agent, 'Mozilla' )}">
<p>display....</p>
</c:if>
But I'd have to do more digging into the standard taglib docs than I
really want to right now. The first one should work without digging
into additional expression language functions.
--David
Post by Nicole Luneburg
Hi,
<%
if(request.getHeader("user-agent").startsWith("Mozilla")) {
%>
display this
<%
} else {
%>
display this
<%
}
%>
I don't know how to do the request bit ...
HttpServletRequest request = new HttpServletRequest();
or is it just request.getHeader
or HttpRequest request = new HttpRequest();
Do i need libraries? If so, how does that work in magnolia?
Can I import stuff in magnolia jsp's like in a normal java project?
That's what I'm lost on!
Thanks
----------------------------------------------------------------
for list details see
http://www.magnolia.info/en/magnolia/developer.html
----------------------------------------------------------------
----------------------------------------------------------------
for list details see
http://www.magnolia.info/en/magnolia/developer.html
----------------------------------------------------------------
Regards,

Nicole Luneburg
Software Developer
--
LISAsoft http://www.lisasoft.com
38 Greenhill Road
Wayville SA 5034
Ph: +61 8 8272 1555
Fax: +61 8 8271 1199
--
The contents of this email are confidential and may be subject to legal or professional privilege and copyright. No representation is made that this email is free of viruses or other defects. If you have received this communication in error, you may not copy or distribute any part of it or otherwise disclose its contents to anyone. Please advise the sender of your incorrect receipt of this correspondence.
--
----------------------------------------------------------------
Ali HAMMADI GHANEM
2007-01-05 08:17:16 UTC
Permalink
Hi Nicole,

I used this solution (a taglib) but I got a problem when I activated the
cache of magnolia. So if you use this solution, you will get problem
when you change your browser (IE to Firefox or ...)

Regards

Ali HAMMADI

----------------------------------------------------------------
Nicole Luneburg
2007-01-07 22:41:28 UTC
Permalink
Thanks Ali,

Thanks for that!
Maybe there is a cache refresh command that can be called ...
I'll look into it if the problem occurs.

Thanks again
Post by Ali HAMMADI GHANEM
Hi Nicole,
I used this solution (a taglib) but I got a problem when I activated
the cache of magnolia. So if you use this solution, you will get
problem when you change your browser (IE to Firefox or ...)
Regards
Ali HAMMADI
----------------------------------------------------------------
for list details see
http://www.magnolia.info/en/magnolia/developer.html
----------------------------------------------------------------
----------------------------------------------------------------
Ali HAMMADI GHANEM
2007-01-08 11:51:30 UTC
Permalink
Hi Nicole,

If you want, you can user a script (in javascript) which can identify
your browser.
This is the link:
http://www.quirksmode.org/js/detect.html

Regards,

Ali

----------------------------------------------------------------

Continue reading on narkive:
Loading...