?? browser.java
字號:
package com.bean;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* <p>Title: 獲取客戶信息</p>
* <p>Description: 通過解析客戶發送的文件頭,獲取客戶信息</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: Browser.java</p>
* @author 杜江
* @version 1.0
*/
public class Browser extends HttpServlet
{
protected HttpServletRequest request;
protected HttpSession session;
protected String userAgent;
protected String company; // 瀏覽器出品公司
protected String name; // 瀏覽器名稱
protected String version; // 瀏覽器版本
protected String mainVersion; // 瀏覽器主版本號
protected String minorVersion; // 瀏覽器小版本號
protected String os; // 客戶使用的操作系統
protected String language = "cn"; // 瀏覽器使用的語言
protected Locale locale; // 客戶所在時區
private Hashtable supportedLanguages; // 客戶所支持的語言
/**
*<br>方法說明:構造器,獲取所有信息
*<br>輸入參數:
*<br>返回類型:
*/
public Browser(HttpServletRequest request, HttpSession session)
{
this.initialize();
this.request = request;
this.session = session;
//主要解析對象:User-Agent
this.setUserAgent(this.request.getHeader("User-Agent"));
this.setCompany();
this.setName();
this.setVersion();
this.setMainVersion();
this.setMinorVersion();
this.setOs();
this.setLanguage();
this.setLocale();
}
/**
*<br>方法說明:初始化支持的語言
*<br>輸入參數:
*<br>返回類型:
*/
public void initialize()
{
this.supportedLanguages = new Hashtable(2);
this.supportedLanguages.put("en", "");
this.supportedLanguages.put("gb", "");
}
/**
*<br>方法說明:獲取瀏覽器信息
*<br>輸入參數:
*<br>返回類型:
*/
public void setUserAgent(String httpUserAgent)
{
this.userAgent = httpUserAgent.toLowerCase();
}
/**
*<br>方法說明:解析瀏覽器出品公司
*<br>輸入參數:
*<br>返回類型:
*/
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";
}
}
/**
*<br>方法說明:獲取公司名稱
*<br>輸入參數:
*<br>返回類型:
*/
public String getCompany()
{
return this.company;
}
/**
*<br>方法說明:解析瀏覽器名稱
*<br>輸入參數:
*<br>返回類型:
*/
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";
}
}
/**
*<br>方法說明:返回瀏覽器名稱
*<br>輸入參數:
*<br>返回類型:
*/
public String getName()
{
return this.name;
}
/**
*<br>方法說明:獲得瀏覽器版本
*<br>輸入參數:
*<br>返回類型:
*/
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(" "));
}
}
/**
*<br>方法說明:返回瀏覽器版本
*<br>輸入參數:
*<br>返回類型:
*/
public String getVersion()
{
return this.version;
}
/**
*<br>方法說明:獲得主版本號
*<br>輸入參數:
*<br>返回類型:
*/
private void setMainVersion()
{
this.mainVersion = this.version.substring(0, this.version.indexOf("."));
}
/**
*<br>方法說明:返回主版本號
*<br>輸入參數:
*<br>返回類型:
*/
public String getMainVersion()
{
return this.mainVersion;
}
/**
*<br>方法說明:獲得小版本號
*<br>輸入參數:
*<br>返回類型:
*/
private void setMinorVersion()
{
this.minorVersion = this.version.substring(this.version.indexOf(".") + 1).trim();
}
/**
*<br>方法說明:返回小版本號
*<br>輸入參數:
*<br>返回類型:
*/
public String getMinorVersion()
{
return this.minorVersion;
}
/**
*<br>方法說明:獲得操作系統名稱
*<br>輸入參數:
*<br>返回類型:
*/
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("windows nt 5.0") > -1 )
{
this.os = "Windows 2000";
}
if (this.userAgent.indexOf("win16") > -1 || this.userAgent.indexOf("windows 3.") > -1)
{
this.os = "Windows 3.x";
}
}
}
/**
*<br>方法說明:返回操作系統名稱
*<br>輸入參數:
*<br>返回類型:
*/
public String getOs()
{
return this.os;
}
/**
*<br>方法說明:獲得瀏覽器接受語言
*<br>輸入參數:
*<br>返回類型:
*/
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);
}
}
}
}
/**
*<br>方法說明:獲得語言語言時區
*<br>輸入參數:
*<br>返回類型:
*/
private String parseLocale(String language)
{
StringTokenizer st = new StringTokenizer(language, "-");
if (st.countTokens() == 2)
{
return st.nextToken();
}
else
{
return language;
}
}
/**
*<br>方法說明:返回語言
*<br>輸入參數:
*<br>返回類型:
*/
public String getLanguage()
{
return this.language;
}
/**
*<br>方法說明:獲得客戶時區
*<br>輸入參數:
*<br>返回類型:
*/
private void setLocale()
{
this.locale = new Locale(this.language, "");
}
/**
*<br>方法說明:返回時區
*<br>輸入參數:
*<br>返回類型:
*/
public Locale getLocale()
{
return this.locale;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -