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