How do I tell what browser is hitting a servlet?
Author: Deron Eriksson
Description: This Java servlet tutorial describes how to use a request header's user-agent to tell what browser is hitting a servlet.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20

Page:    1 2 >

Short Answer:

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


Under normal conditions, you can obtain information about the type of browser hitting a servletW by examining the "user-agent" value in the request header. This is demonstrated in this tutorial. Note, however, that this value is not guaranteed to be correct, although it is usually correct, since a browser or other client can send any string it wants to send for the "user-agent" in the request header, and it actually isn't even obligated to send any "user-agent" string at all.

It's also possible for the user-agent string to get changed or altered before it arrives at a servlet. In complex technology stacks with load balancers and multiple web servers and application servers, the user-agent string may get changed before arriving at the servlet.

Analysis of user-agent strings can be difficult, since they come in many flavors. For instance, if you install various software patches on Windows machines, this typically adds various entries to the user-agent string for Internet Explorer. Additionally, there are so many flavors of Mozilla and Firefox browsers that it can be extremely difficult to differentiate the different browsers.

In general, it's fairly easy to tell the platforms of the browsers hitting a servlet (PC, Mac, Solaris, Linux, etc.), and it's easy to differentiate Internet Explorer browser hits from Firefox-style browser hits. However, going to a greater level of detail can be a challenge. If you need generalized user statistics with little work on your part, I recommend using one of the various ApacheSW web log analysis tools that can be found on the web.

The tutorial project consists of one servlet and a web.xmlW file.

'tomcat-demo' project

In Tomcat's server.xml file, I added a Context entry for 'tomcat-demo', the name of the test project.

<Context docBase="C:\projects\workspace\tomcat-demo\web" path="/tomcat-demo" reloadable="true"/>

I created a Debug Configuration in EclipseSW for 'tomcat-demo', using the Bootstrap class (after adding the various TomcatSW jarW file libraries to the project's build path).

'tomcat-demo' Debug Configuration

My working directory is the Tomcat home directory.

Working directory set to Tomcat home directory

The web.xml file contains a servlet entry for TestServlet, which is mapped to /test.


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="tomcat-demo" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation="">

The TestServlet class has doGet() implemented. It enumerates over the various header names in the request header, and then it obtains the header value for each header name. It returns these name/value pairs to the browser by writing them to the response object.

package test;

import java.util.Enumeration;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		out.println("This is the Test Servlet");

		Enumeration headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String headerName = (String) headerNames.nextElement();
			out.print("<br/>Header Name: <em>" + headerName);
			String headerValue = request.getHeader(headerName);
			out.print("</em>, Header Value: <em>" + headerValue);


(Continued on page 2)

Page:    1 2 >