Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : J2EE : JSP : Creating a Browser Detection JavaBean
 
<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.

<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.


Previous ( 1 Gone )( No Further Pages )

See all comments and questions (post-ad) posted for this tutorial.


Related Articles
  1. What are JavaBeans ?
  2. Calling JavaBeans from a JSP Page

Comments/Questions ( Threads: 1, Comments: 1 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. give me code ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  2. detecting browser

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.