动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   Asp → 阅读文章

 使用JSP读取客户端信息

作者来源: 
阅读 1228 人次 , 2006-3-29 4:22:00 


  请阅读以下代码。如果你的使用要求不同,可对这些代码加以很方便的修改。这些代码可以使你获得:
公司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;
}
}

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:在jsp中发送email
· 下一篇:如何使用JSP+MySQL创建留言本
· Othello游戏源程序
· 如何只安装.NET的文档,而不安装.NET的FrameWork
· IIS服务器的特性
· 词语搭配游戏的制作(ASP)三
· 制作我们自己的Ebay(拍卖系统EN) - Managing Bids - Page 5


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077