Using JSP to read client information _jsp programming
Last Update:2017-01-18
Source: Internet
Author: User
Using JSP to read client information
Please read the following code. If your usage requirements are different, you can easily modify the code. This code allows you to obtain:
Company, user name, version version,main Version,minor
Operating system (not completed!), language Language,locale and so on.
To create a new JSP file:
Please add the following class file to Classpath (you want to build the same directory structure--de.hunsicker.http.util, and of course you can adjust the name of the package 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)
{
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?nderkü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;
}
}