Pin It

How to check whether which Web-Browser have been used to run JSP and Servlets ?.



A simple Web Application demonstrating how to check whether which Web-Browser have been used to run JSP and Servlets


Using Java Server Pages : browser.jsp


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Browser Test</title>
</head>
<body>

 <h2>
  Let's have a browser test today : <br> The Web-Browser you are
  using today is :

 </h2>

 <hr>

 <%
  // JSP page has request implicit object has a method which brings
  // with it headers coming in request. The method name is
  // request.getHeader(). In order to check which browser 
  // we are using we extract user-agent info from the headers
  String browser = request.getHeader("user-agent");
  if (browser.indexOf("MSIE") != -1) {
 %>
 <h2>You are using Microsoft Internet Explorer today</h2>
 <%
  }
  else if (browser.indexOf("Chrome") != -1) {
 %>
 <h2>You are using Google Chrome today</h2>
 <%
  }
  else if (browser.indexOf("Firefox") != -1) {
 %>
 <h2>You are using Mozilla Firefox today</h2>
 <%
  }
  else {
 %>
 <h2>You are using any other browser</h2>
 <%
  }
 %>
 <hr>
</body>
</html>

Using Servlets : BrowserTestServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/BrowserTestServlet")
public class BrowserTestServlet extends HttpServlet {

 protected void doGet(HttpServletRequest request,
   HttpServletResponse response)
     throws
     ServletException, IOException {

  String browser = request.getHeader("user-agent");

  PrintWriter out = response.getWriter();
  out.println("<br/><h2>Let's have a browser test today : " 
    + "<br> The Web-Browser you are using today is : ");
  out.println("<br/></hr>");



  out.println("<br/>");
  if(browser.indexOf("MSIE")!=-1)
   out.println("<h2>You are using Microsoft Internet Explorer</h2>");
  else if(browser.indexOf("Chrome")!=-1)
   out.println("<h2>You are using Google Chrome</h2>");
  else if(browser.indexOf("Firefox")!=-1)
   out.println("<h2>You are using Mozilla Firefox</h2>");
  else
   out.println("<h2>You are using any other browser</h2>");
 }

}



Output of the Servlet and JSP are same as below:

In a Microsoft Explorer Window output is :
























In a Google Chrome Window output is :


















In a Mozilla Firefox Window output is :







Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot