Use JSP to read client information
Read the following code. If you have different requirements, you can easily modify these codes. These codes give you the following:
Company, user name, version, main version, minor version
Operating System (unfinished !), Language, locale, etc.
Create a new JSP file:
Add the following class files to classpath (you need to create the same directory structure -- de. hunsicker. http. util, you can also adjust the package name by yourself .!) :
Package de. hunsicker. http. util;
Import java. util .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Public class Browser extends HttpServlet
{
Protected HttpServletRequest request;
Protected HttpSession session;
Protected String userAgent;
Protected String company; // Firmenname des Herstellers
Protected String name; // Bezeichnung des Browsers
Protected String version; // Version
Protected String mainVersion; // Hauptversion
Protected String minorVersion; // Unterversion
Protected String OS; // Betriebssystem
Protected String language = \ "de \"; // Sprachcode Standard
Protected Locale locale; // Locale-Objekt mit den aktuellen
// Spracheinstellungen
Private Hashtable supportedLanguages; // Untersttzte Sprachen
Public Browser (HttpServletRequest request, HttpSession session)
{
This. initialize ();
This. request = request;
This. session = session;
This. setUserAgent (this. request. getHeader (\ "User-Agent \"));
This. setCompany ();
This. setName ();
This. setVersion ();
This. setMainVersion ();
This. setMinorVersion ();
This. setOs ();
This. setLanguage ();
This. setLocale ();
}
Public void initialize ()
{
This. supportedLanguages = new Hashtable (2 );
This. supportedLanguages. put (\ "en \",\"\");
This. supportedLanguages. put (\ "de \",\"\");
}
Public void setUserAgent (String httpUserAgent)
{
This. userAgent = httpUserAgent. toLowerCase ();
}
Private void setCompany ()
{
If (this. userAgent. indexOf (\ "msie \")>-1)
{
This. company = \ "Microsoft \";
}
Else if (this. userAgent. indexOf (\ "opera \")>-1)
{
This. company = \ "Opera Software \";
}
Else if (this. userAgent. indexOf (\ "mozilla \")>-1)
{
This. company = \ "Netscape Communications \";
}
Else
{
This. company = \ "unknown \";
}
}
/**
* Liefert den Firmennamen des Herstellers des verwendeten Browsers.
*/
Public String getCompany ()
{
Return this. company;
}
Private void setName ()
{
If (this. company = \ "Microsoft \")
{
This. name = \ "Microsoft Internet Explorer \";
}
Else if (this. company = \ "Netscape Communications \")
{
This. name = \ "Netscape Navigator \";
}
Else if (this. company = \ "Operasoftware \")
{
This. name = \ "Operasoftware Opera \";
}
Else
{
This. name = \ "unknown \";
}
}
/**
* Liefert den Namen des verwendeten Browsers.
*/
Public String getName ()
{
Return this. name;
}
Private void setVersion ()
{
Int tmpPos;
String tmpString;
If (this. company = \ "Microsoft \")
{
String str = this. userAgent. substring (this. userAgent. indexOf (\ "msie \") + 5 );
This. version = str. substring (0, str. indexOf (\";\"));
}
Else
{
TmpString = (this. userAgent. substring (tmpPos = (this. userAgent. indexOf (\ "/\") + 1, tmpPos + this. userAgent. indexOf (\"\"))). trim ();
This. version = tmpString. substring (0, tmpString. indexOf (\"\"));
}
}
/**
* Liefert die Versionsnummer des verwendeten Browsers.
*/
Public String getVersion ()
{
Return this. version;
}
Private void setMainVersion ()
{
This. mainVersion = this. version. substring (0, this. version. indexOf (\".\"));
}
/**
* Liefert die Hauptversionsnummer des verwendeten Browsers.
*/
Public String getMainVersion ()
{
Return this. mainVersion;
}
Private void setMinorVersion ()
{
This. minorVersion = this. version. substring (this. version. indexOf (\ ". \") + 1). trim ();
}
/**
* Liefert die Unterversionsnummer des verwendeten Browsers.
*/
Public String getMinorVersion ()
{
Return this. minorVersion;
}
Private void setOs ()
{
If (this. userAgent. indexOf (\ "win \")>-1)
{
If (this. userAgent. indexOf (\ "windows 95 \")>-1 | this. userAgent. indexOf (\ "win95 \")>-1)
{
This. OS = \ "Windows 95 \";
}
If (this. userAgent. indexOf (\ "windows 98 \")>-1 | this. userAgent. indexOf (\ "win98 \")>-1)
{
This. OS = \ "Windows 98 \";
}
If (this. userAgent. indexOf (\ "windows nt \")>-1 | this. userAgent. indexOf (\ "winnt \")>-1)
{
This. OS = \ "Windows NT \";
}
If (this. userAgent. indexOf (\ "win16 \")>-1 | this. userAgent. indexOf (\ "windows 3. \")>-1)
{
This. OS = \ "Windows 3. x \";
}
}
}
/**
* Liefert den Namen des Betriebssystems.
*/
Public String getOs ()
{
Return this. OS;
}
Private void setLanguage ()
{
String prefLanguage = this. request. getHeader (\ "Accept-Language \");
If (prefLanguage! = Null)
{
String language = null;
StringTokenizer st = new StringTokenizer (prefLanguage ,\",\");
Int elements = st. countTokens ();
For (int idx = 0; idx elements; idx ++)
{
If (this. supportedLanguages. containsKey (language = st. nextToken ())))
{
This. language = this. parseLocale (language );
}
}
}
}
/*
* Hilfsfunktion fr setLanguage ().
*/
Private String parseLocale (String language)
{
StringTokenizer st = new StringTokenizer (language ,\"-\");
If (st. countTokens () = 2)
{
Return st. nextToken ();
}
Else
{
Return language;
}
}
/**
* Liefert das L? Nderürzel der vom Benutzer
* Bevorzugten Sprache.
*/
Public String getLanguage ()
{
Return this. language;
}
Private void setLocale ()
{
This. locale = new Locale (this. language ,\"\");
}
/**
* Liefert ein Locale-Objekt mit der Sprach-Prferenz des verwendeten Browsers
*/
Public Locale getLocale ()
{
Return this. locale;
}
}