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"); b1.addActionListener(this); add(tf);add(b1); } public void actionPerformed(ActionEvent ae) { if (ae.getSource() == b1) { JSObject win = (JSObject)JSObject.getWindow(this); tf.setText((String)win.eval("getUserAgent();")); } } }
<html> <head> <script> function getUserAgent() { return navigator.userAgent; } </script> </head><body> <applet code="GetUserAgent.class" MAYSCRIPT height=100 width=500></Applet> </body> </html>
You need to parse the returned value to detect browser type. Look at this utility, http://code.google.com/p/user-agent-utils/ or at this code : http://nerds.palmdrive.net/useragent/code.html 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.In a JSP/EL
<applet ... > <param ... > <param name="userAgent" value="${header['user-agent']}" /> </applet>
String userAgent = getParameter("userAgent");