<html>
<head>
<title>DetectBrowser Bean</title>
<style>
p, ul, li { font-family:Trebuchet MS,Verdana; font-size:12pt; }
</style>
</head>
<body>
<p align="center"><u><b>DetectBrowser Bean</b></u></p>
<jsp:useBean id="db" class="com.stardeveloper.bean.test.DetectBrowser"
scope="session">
<jsp:setProperty name="db" property="request" value="<%= request %>" />
</jsp:useBean>
<p>User-Agent : <b>
<jsp:getProperty name="db" property="useragent" />
</b></p>
<p>.NET Enabled : <b>
<jsp:getProperty name="db" property="netEnabled" />
</b></p>
<ul>
<li>IE : <b><%= db.isIE() %></b></li>
<li>NS6 : <b><%= db.isNS6() %></b></li>
<li>NS4 : <b><%= db.isNS4() %></b></li>
</ul>
<p style="padding-left:30;"><% if(db.isIE()) { %>
Internet Explorer - By far the best browser you can get.
<% } else if(db.isNS6()) { %>
Netscape 6 - If you cannot run Internet Explorer, use Netscape 6.
<% } else if(db.isNS4()) { %>
Netscape 4 - You have got to upgrade your browser!
<% } else { %>
Other - Your browser could not be detected by DetectBrowser Bean.
Please report to
<a href="mailto:faisal@stardeveloper.com">faisal@stardeveloper.com</a>
and tell him this message that you got along with the User-Agent
string above.
<% } %></p>
</body>
</html>
Explanation
Above code is not difficult at all. We have already studied <jsp:useBean>,
<jsp:setProperty> and <jsp:getProperty> tags in the Calling a JavaBean from a JSP page article.
We first declare and instantiate the DetectBrowser JavaBean using the <jsp:useBean>
tag. Then set the request property of DetectBrowser bean to the JSP page's HttpServletRequest
object using the <jsp:setProperty>. Notice that we have set the scope for our
DetectBrowser JavaBean to session, so that this bean is created only once during the
session life time of the user.
<jsp:useBean id="db" class="com.stardeveloper.bean.test.DetectBrowser"
scope="session">
<jsp:setProperty name="db" property="request" value="<%= request %>" />
</jsp:useBean>
Display the User-Agent header to the user :
<p>User-Agent : <b>
<jsp:getProperty name="db" property="useragent" />
</b></p>
Display the .NET Enabled status to the user :
<p>.NET Enabled : <b>
<jsp:getProperty name="db" property="netEnabled" />
</b></p>
Till now we have been using <jsp:getProperty> tag to retrieve property values
from the bean, from now onwards we will access these bean methods directly.
Display the browser status retrieved by our JavaBean to the user.
<ul>
<li>IE : <b><%= db.isIE() %></b></li>
<li>NS6 : <b><%= db.isNS6() %></b></li>
<li>NS4 : <b><%= db.isNS4() %></b></li>
</ul>
Now again using the DetectBrowser.isXX() methods we display certain text to the user.
I have added my own messages, you can substitute your own.
<p style="padding-left:30;"><% if(db.isIE()) { %>
Internet Explorer - By far the best browser you can get.
<% } else if(db.isNS6()) { %>
Netscape 6 - If you cannot run Internet Explorer, use Netscape 6.
<% } else if(db.isNS4()) { %>
Netscape 4 - You have got to upgrade your browser!
<% } else { %>
Other - Your browser could not be detected by DetectBrowser Bean.
Please report to
<a href="mailto:faisal@stardeveloper.com">faisal@stardeveloper.com</a>
and tell him this message that you got along with the User-Agent
string above.
<% } %></p>
We are done with DetectBrowser.jsp file.
Summary
In this step by step tutorial we created a DetectBrowser JavaBean which enables us
to detect user browser and render appropriate HTML. Since different browsers support
HTML and CSS differently, this JavaBean can be of lot of help to you.
The DetectBrowser.jsp JSP page was also pretty simple and used <jsp:useBean>,
<jsp:setProperty> and <jsp:getProperty> tags to instantiate, set and get
different property values from the bean.
-
Click here to see the DetectBrowser bean in action.