Browser best practices in Java

You can use New Relic Browser with New Relic APM for Java to measure end-user load times. This document explains best practices for setting up Browser monitoring. For information on how to set up Browser monitoring for your Java app, see New Relic Browser and the Java agent.

Ensure you are using the Apache Jasper compiler.

The New Relic Java agent only auto-instruments pages compiled with the Apache Jasper compiler. The following application servers use the Jasper compiler by default:

  • Tomcat
  • Jetty
  • Glassfish
  • JBoss 4

Place meta tags immediately after the initial head tag.

Recommendation: Place all <meta> tags immediately after the initial <head> tag. Some meta tags have requirements on how close they need to be to the beginning of an HTML document. The character encoding must be declared within the first 512 bytes of the HTML document. If the New Relic header is placed before the character encoding, it is possible for the character encoding to then be outside that 512 byte limit. Placing the character encoding meta tag immediately after the <head> tag will ensure that the New Relic header is positioned after the character encoding.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
NEW RELIC HEADER
 . . .
</head>

The X-UA-Compatible meta tag should be within the meta tags immediately after the <head> tag. This tag allows page authors to set the document mode used for rending the page in Internet Explorer. This tag should be placed before any script tags. If the New Relic header is positioned before the X-UA-Compatible meta tag, the page might display incorrectly in Internet Explorer.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=9">
NEW RELIC HEADER
 . . .
</head>

Avoid if ... else statements in the head section.

The auto instrumentation script does not recognize if ... else statements within a JSP page. If you have an if ... else block before your first non-meta, non-title tag, the New Relic header might be placed in the incorrect position in the page. For example, this code could potentially cause the auto instrumentation script to be inserted before the meta tags:

if (expression) {
  <nonmeta tag>
}
<meta tag>
<meta tag>

Avoid expressions with the less than sign in the head section.

The New Relic Java agent looks for the open angle bracket < to mark the beginning of an HTML tag. This means if you have an expression using a less than sign, then you will either need to change your expression to use a greater than sign or use manual instrumentation. For example:

<head>
<% for (i = 0; i < variable; i++)
. . . 
%>

For more help

Additional documentation resources include:

Join the discussion about Java monitoring in the New Relic Online Technical Community! The Technical Community is a public platform to discuss and troubleshoot your New Relic toolset.

If you need additional help, get support at support.newrelic.com.