使用JSP读取客户端信息
程序员文章站
2024-02-11 17:10:58
使用jsp读取客户端信息 请阅读以下代码。如果你的使用要求不同,可对这些代码加以很方便的修改。这些代码可以使你获得: 公司company, 用户name,版本versi...
使用jsp读取客户端信息
请阅读以下代码。如果你的使用要求不同,可对这些代码加以很方便的修改。这些代码可以使你获得:
公司company, 用户name,版本version,main version,minor version
操作系统(未完成!),语言language,locale等。
建立一个新的jsp文件:
请将下列class文件加入classpath (你要建立同样的目录结构-- de.hunsicker.http.util,当然也可以自己调节包的名称。!):
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?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;
}
}
请阅读以下代码。如果你的使用要求不同,可对这些代码加以很方便的修改。这些代码可以使你获得:
公司company, 用户name,版本version,main version,minor version
操作系统(未完成!),语言language,locale等。
建立一个新的jsp文件:
请将下列class文件加入classpath (你要建立同样的目录结构-- de.hunsicker.http.util,当然也可以自己调节包的名称。!):
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?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;
}
}
上一篇: 小程序用canvas生成图片
下一篇: SQL点滴24 监测表的变化
推荐阅读
-
使用JSP读取客户端信息
-
jsp 获取客户端的浏览器和操作系统信息
-
ZK中使用JS读取客户端txt文件内容问题
-
使用python批量读取word文档并整理关键信息到excel表格的实例
-
Python读取图片EXIF信息类库介绍和使用实例
-
Python使用pyshp库读取shapefile信息的方法
-
Golang使用第三方包viper读取yaml配置信息操作
-
C#使用Linq to csv读取.csv文件数据2_处理含有非列名数据的方法(说明信息等)
-
使用nodejs实现OData的batch操作在Marketing Cloud里读取contact信息 nodejsSAP成都研究院SAP Cloud PlatformSAP云平台Marketing Cloud
-
jsp中如何获取客户端的浏览器和操作系统信息