Read client information using JSP

xiaoxiao2021-03-06  94

Read client information using JSP

Please read the following code. If your use is different, you can make it easy to modify these codes. These code can get you:

Company companies, users name, version version, main version, minor version

Operating system (unfinished!), Language Language, Locale, etc.

Create a new JSP file:

<% @ page language = "java" import = "de.hunsicker.http.util. *"%>

<%

Browser Eins = New Browser (Request, Session);

Out.println (Eins.getVersion ());

%>

Add the following Class files to ClassPath (you want to build the same directory structure - de.hunsicker.http.util, of course, you can also adjust the name of the package.!):

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 uSgene;

Protected string company; // firmenname des harstellers

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-object mit den aktuellen

// spracheinstellungen

Private HashTable Supportedlanguages; // Unterstzte 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. IUSERAGENT = httpusemergent.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 verweten 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 Verweeten Browsers.

* /

Public string getName ()

{

Return this.name;

}

Private void setversion ()

{

INT TMPOS;

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 (Tmpos = (this.usemergent.indexof ("/")) 1, TMPPOS THISUSERAGENT.INDEXOF (""))). TRIM ();

THISTRING.SUBSTRING (0, tmpstring.indexof ("));

}

/ **

* Liefert Die VersionsNummer Des Verweeten Browsers.

* /

Public string getversion ()

{

Return this.version;

}

Private void setmainversion ()

{

THIS.MAINVERSION = this.version.substring (0, this.version.indexof ("."));

}

/ **

* Liefert Die HauptversionsNummer Des Verweeten Browsers.

* /

Public string getmainversion ()

{

Return this.mainversion;

}

Private void setminorversion ()

{

THIS.MINORVERSION = this.version.substring (this.version.indexof (".") 1) .trim ();

}

/ **

* Liefert Die UnterVersionsNummer Des Verweeten 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 ()

{

THISLOCALE = New local (this.language, "");

}

/ **

* Liefert Ein Locale-Objekt Mit der sprach-prferenz des verweten Browsers

* /

Public Locale getLocale ()

{

Return THISLOCALE;

}

}

转载请注明原文地址:https://www.9cbs.com/read-96839.html

New Post(0)