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

5 comments:

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

    ReplyDelete
  2. Can you support this too:

    http://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

    ReplyDelete
  3. In the JSP code I think this line needs to be modified :
    BrowserRecognitionModel model=(BrowserRecognitionModel)request.getAttribute("model");

    to:
    BrowserRecognitionModel model=(BrowserRecognitionModel)request.getAttribute("user-agent");

    ReplyDelete
  4. It is missing safari !

    ReplyDelete
  5. Thank You and that i have a keen provide: Whole Home Renovation Cost dream home renovations

    ReplyDelete

Total Pageviews

Followers


Labels

Popular Posts

free counters