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 :
Output in Chrome

Output in Firefox








--------------------------------------------------------------------------------------------------------------------------
SOURCE CODE
--------------------------------------------------------------------------------------------------------------------------
/* 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

1 comment:

  1. I have a problem with BrowserRecogntionServlet . line 11: @Webservlet("/detect.html");

    ReplyDelete

Total Pageviews

Subscribe via Email

Followers


Popular Posts

About Me

My Photo

I am a student of BTech Computer Science Engineering from RCCIIT,Kolkata. I am a crazy lover of Java and wants to settle as a Java developer. I have a seven years Java experience with an application developer experience for 2 years. Recently from March 2012 I am a registered S40 app developer for Nokia and has corrected an app of them. I am currently writing blogs to encourage and grow interest in all those who don't know or learning Java.