Friday, October 26, 2012
--------------------UPDATE-------------------
I have updated my post so that now it can detect IE 11. This modification was necessary as the user-agent of Internet Explorer 11.0 was changed. So check the new code which has an additional if-else block in class BrowserRecognitionModel .
---------------------------------------------------
Today I am going to post program which will be able to detect client's browser details i.e. browser name and version using servlets and JSPs. First of all, user-agent header is extracted and then string processing is done to obtain browser name and version.
Benefits : You will be able to detect which browser is being used by client. Moreover the whole application is being written following the MVC pattern.
BrowserRecogntionServlet.java : contains the code for extracting user-agent and hands it over to the model class and then forwards the result to the JSP and thus acting as controller.
BrowserRecognitionModel.java : contains the code that processes the string and gets the browser version and name.
browserDetails.jsp : contains the code for showing result.
Screenshots of output as seen in Chrome and Firefox :
--------------------------------------------------------------------------------------------------------------------------
I have updated my post so that now it can detect IE 11. This modification was necessary as the user-agent of Internet Explorer 11.0 was changed. So check the new code which has an additional if-else block in class BrowserRecognitionModel .
---------------------------------------------------
Today I am going to post program which will be able to detect client's browser details i.e. browser name and version using servlets and JSPs. First of all, user-agent header is extracted and then string processing is done to obtain browser name and version.
Benefits : You will be able to detect which browser is being used by client. Moreover the whole application is being written following the MVC pattern.
BrowserRecogntionServlet.java : contains the code for extracting user-agent and hands it over to the model class and then forwards the result to the JSP and thus acting as controller.
BrowserRecognitionModel.java : contains the code that processes the string and gets the browser version and name.
browserDetails.jsp : contains the code for showing result.
Screenshots of output as seen in Chrome and Firefox :
Output in Chrome |
Output in Firefox |
--------------------------------------------------------------------------------------------------------------------------
SOURCE CODE
--------------------------------------------------------------------------------------------------------------------------
/* Code for servlet(BrowserRecogntionServlet.java) */
/* Code for servlet(BrowserRecogntionServlet.java) */
package detector.servlets; import detector.model.BrowserRecognitionModel; import java.io.PrintWriter; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.annotation.WebServlet; @WebServlet("/detect.html") public class BrowserRecognitionServlet extends HttpServlet{ @Override public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException{ String userAgent=req.getHeader("user-agent"); /*passing data to model class */ BrowserRecognitionModel browserDetails=new BrowserRecognitionModel(userAgent); /* setting attribute to request for the JSP */ req.setAttribute("model",browserDetails); /* forwarding to JSP for output */ RequestDispatcher view=req.getRequestDispatcher("/browserDetails.jsp"); view.forward(req,res); } }
/* Code for model class(BrowserRecognitionModel.java) */
package detector.model; public class BrowserRecognitionModel { private String userAgent,browserName,browserVer; public BrowserRecognitionModel(String userAgent){ this.userAgent=userAgent; process(); } private void process(){ browserName="unknown"; browserVer="unknown"; if(userAgent.contains("Chrome")){ //checking if Chrome String substring=userAgent.substring(userAgent.indexOf("Chrome")).split(" ")[0]; browserName=substring.split("/")[0]; browserVer=substring.split("/")[1]; } else if(userAgent.contains("Firefox")){ //Checking if Firefox String substring=userAgent.substring(userAgent.indexOf("Firefox")).split(" ")[0]; browserName=substring.split("/")[0]; browserVer=substring.split("/")[1]; } else if(userAgent.contains("MSIE")){ //Checking if Internet Explorer String substring=userAgent.substring(userAgent.indexOf("MSIE")).split(";")[0]; browserName=substring.split(" ")[0]; browserVer=substring.split(" ")[1]; } else if(userAgent.contains("rv")){ //checking if Internet Explorer 11 String substring=userAgent.substring(userAgent.indexOf("rv"),userAgent.indexOf(")")); browserName="IE"; browserVer=substring.split(":")[1]; } } public String getName(){ return browserName; //returning browser name } public String getVersion(){ return browserVer; //returning browser version } }
/* Code for JSP(browserDetails.jsp) */
<html> <head><title>Browser Details</title></head> <%@ page import="detector.model.BrowserRecognitionModel" %> <body> <h1>Browser Details</h1> <% BrowserRecognitionModel model=(BrowserRecognitionModel)request.getAttribute("model"); String browserName=model.getName(); String version=model.getVersion(); %> <b>Browser Name : </b><%= browserName %></br> <b>Browser Version : </b><%= version %> </body> </html>
--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD LINKS
--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD the application(rar) from Mediafire
DOWNLOAD the application(rar) from 4shared
DOWNLOAD the application(rar) from Mediafire
DOWNLOAD the application(rar) from 4shared
Labels:J2EE
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
I have a problem with BrowserRecogntionServlet . line 11: @Webservlet("/detect.html");
ReplyDeleteCan you support this too:
ReplyDeletehttp://msdn.microsoft.com/en-us/library/ie/hh869301(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/ie/hh869301(v=vs.85).aspx
In the JSP code I think this line needs to be modified :
ReplyDeleteBrowserRecognitionModel model=(BrowserRecognitionModel)request.getAttribute("model");
to:
BrowserRecognitionModel model=(BrowserRecognitionModel)request.getAttribute("user-agent");
It is missing safari !
ReplyDeleteThank You and that i have a keen provide: Whole Home Renovation Cost dream home renovations
ReplyDelete