Detect browser type from an Applet (this howto is deprecated)Tag(s): DEPRECATED

Call a Javascript from the Applet
With LiveConnect, we call a javascript to return the "User-Agent".

To compile, make sure that you have the plugin.jar in the classpath (or Eclipse Build Path). This jar is usually located in [JRE]/lib folder. During runtime, it's already included by the Java plugin so you don't have to worry about that.

The Java class

import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import netscape.javascript.*;

public class GetUserAgent extends Applet
implements ActionListener {

  private static final long serialVersionUID = 1L;

  TextField tf;
  Button b1;
  JSObject win;

  public void init(){
    setLayout(new FlowLayout());
    tf = new TextField(35);
    b1 = new Button("get useragent from javascript");


  public void actionPerformed(ActionEvent ae) {
    if (ae.getSource() == b1) {
      JSObject win = (JSObject)JSObject.getWindow(this);
Don't forget the MAYSCRIPT parameter in the APPLET tag, it's required since the Applet is using the JSObject to interact with the Javascript.
 function getUserAgent() {
   return navigator.userAgent;
<applet code="GetUserAgent.class" MAYSCRIPT height=100 width=500></Applet>
Try it here

You need to parse the returned value to detect browser type. Look at this utility, or at this code : to get the idea.

Get the value from the server-side
When serving the HTML containing the Applet, you add the User-Agent as parameter for the Applet.


<applet ... >
   <param ... >
   <param name="userAgent" value="${header['user-agent']}" />
and in the Applet
String userAgent = getParameter("userAgent");
and parse the returned value...
blog comments powered by Disqus