当前位置: 首页 > 图文教程 > 网络编程 > JSP > 使用JSP读取客户端信息

JSP
我认为JSP有问题(上)
我认为JSP有问题(下)
jsp“抓”网页代码的程序
关于在bean里面打印html的利弊看法
bean里面如何打印到html页面
jdbc3中的RowSet 接口规范
Apusic Application Server1.0中jsp源代码泄漏漏洞
Unify的eWave ServletExec拒绝服务漏洞
通过提交超长的GET请求导致IBM HTTP Server远程溢出
在HTTP请求中添加特殊字符导致暴露JSP源代码文件
Resin 1.2 重要源代码暴露漏洞
多中WEB服务器的通用JSp源代码暴露漏洞
Tomcat 暴露JSP文件内容
IBM WebSphere Application Server 暴露JSP文件内容
JRun 2.3.x 范例文件暴露站点安全信息
BEA WebLogic 暴露源代码漏洞
IBM WebSphere Application Server 3.0.2 存在暴露源代码漏洞
Tomcat 3.1 存在暴露网站路径问题
Sun Java Web Server 能让攻击者远程执行任意命令
Netscape 修复 JAVA 安全漏洞

使用JSP读取客户端信息


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-04   浏览: 62 ::
收藏到网摘: n/a

请阅读以下代码。如果你的使用要求不同,可对这些代码加以很方便的修改。这些代码可以使你获得: 
公司company, 用户name,版本version,main version,minor version  
操作系统(未完成!),语言language,locale等。  
建立一个新的JSP文件:  
<%@ page language="java" import="de.hunsicker.http.util.*"%> 
<% 
Browser eins = new Browser(request, session); 
out.println(eins.getVersion()); 
%> 
请将下列class文件加入classpath (你要建立同样的目录结构-- de.hunsicker.http.util,当然也可以自己调节包的名称。!):  
  1. package de.hunsicker.http.util; 
  2. import java.util.*; 
  3. import javax.servlet.*; 
  4. import javax.servlet.http.*; 
  5. public class Browser extends HttpServlet 
  6. protected HttpServletRequest request; 
  7. protected HttpSession session; 
  8. protected String userAgent; 
  9. protected String company; // Firmenname des Herstellers 
  10. protected String name; // Bezeichnung des Browsers 
  11. protected String version; // Version 
  12. protected String mainVersion; // Hauptversion 
  13. protected String minorVersion; // Unterversion 
  14. protected String os; // Betriebssystem 
  15. protected String language = "de"// Sprachcode Standard 
  16. protected Locale locale; // Locale-Objekt mit den aktuellen 
  17. // Spracheinstellungen 
  18. private Hashtable supportedLanguages; // Untersttzte Sprachen 
  19. public Browser(HttpServletRequest request, HttpSession session) 
  20. this.initialize(); 
  21. this.request = request; 
  22. this.session = session; 
  23. this.setUserAgent(this.request.getHeader("User-Agent")); 
  24. this.setCompany(); 
  25. this.setName(); 
  26. this.setVersion(); 
  27. this.setMainVersion(); 
  28. this.setMinorVersion(); 
  29. this.setOs(); 
  30. this.setLanguage(); 
  31. this.setLocale(); 
  32. public void initialize() 
  33. this.supportedLanguages = new Hashtable(2); 
  34. this.supportedLanguages.put("en"""); 
  35. this.supportedLanguages.put("de"""); 
  36. public void setUserAgent(String httpUserAgent) 
  37. this.userAgent = httpUserAgent.toLowerCase(); 
  38. private void setCompany() 
  39. if (this.userAgent.indexOf("msie") > -1) 
  40. this.company = "Microsoft"
  41. else if (this.userAgent.indexOf("opera") > -1) 
  42. this.company = "Opera Software"
  43. else if (this.userAgent.indexOf("mozilla") > -1) 
  44. this.company = "Netscape Communications"
  45. else 
  46. this.company = "unknown"
  47. /** 
  48. * Liefert den Firmennamen des Herstellers des verwendeten Browsers. 
  49. */ 
  50. public String getCompany() 
  51. return this.company; 
  52. private void setName() 
  53. if (this.company == "Microsoft"
  54. this.name = "Microsoft Internet Explorer"
  55. else if (this.company == "Netscape Communications"
  56. this.name = "Netscape Navigator"
  57. else if (this.company == "Operasoftware"
  58. this.name = "Operasoftware Opera"
  59. else 
  60. this.name = "unknown"
  61. /** 
  62. * Liefert den Namen des verwendeten Browsers. 
  63. */ 
  64. public String getName() 
  65. return this.name; 
  66. private void setVersion() 
  67. int tmpPos; 
  68. String tmpString; 
  69. if (this.company == "Microsoft"
  70. String str = this.userAgent.substring(this.userAgent.indexOf("msie") + 5); 
  71. this.version = str.substring(0, str.indexOf(";")); 
  72. else 
  73. tmpString = (this.userAgent.substring(tmpPos = (this.userAgent.indexOf("/")) + 1, tmpPos + this.userAgent.indexOf(" "))).trim(); 
  74. this.version = tmpString.substring(0, tmpString.indexOf(" ")); 
  75. /** 
  76. * Liefert die Versionsnummer des verwendeten Browsers. 
  77. */ 
  78. public String getVersion() 
  79. return this.version; 
  80. private void setMainVersion() 
  81. this.mainVersion = this.version.substring(0, this.version.indexOf(".")); 
  82. /** 
  83. * Liefert die Hauptversionsnummer des verwendeten Browsers. 
  84. */ 
  85. public String getMainVersion() 
  86. return this.mainVersion; 
  87. private void setMinorVersion() 
  88. this.minorVersion = this.version.substring(this.version.indexOf(".") + 1).trim(); 
  89. /** 
  90. * Liefert die Unterversionsnummer des verwendeten Browsers. 
  91. */ 
  92. public String getMinorVersion() 
  93. return this.minorVersion; 
  94. private void setOs() 
  95. if (this.userAgent.indexOf("win") > -1) 
  96. if (this.userAgent.indexOf("windows 95") > -1 || this.userAgent.indexOf("win95") > -1) 
  97. this.os = "Windows 95"
  98. if (this.userAgent.indexOf("windows 98") > -1 || this.userAgent.indexOf("win98") > -1) 
  99. this.os = "Windows 98"
  100. if (this.userAgent.indexOf("windows nt") > -1 || this.userAgent.indexOf("winnt") > -1) 
  101. this.os = "Windows NT"
  102. if (this.userAgent.indexOf("win16") > -1 || this.userAgent.indexOf("windows 3.") > -1) 
  103. this.os = "Windows 3.x"
  104. /** 
  105. * Liefert den Namen des Betriebssystems. 
  106. */ 
  107. public String getOs() 
  108. return this.os; 
  109. private void setLanguage() 
  110. String prefLanguage = this.request.getHeader("Accept-Language"); 
  111. if (prefLanguage != null
  112. String language = null
  113. StringTokenizer st = new StringTokenizer(prefLanguage, ","); 
  114. int elements = st.countTokens(); 
  115. for (int idx = 0; idx elements; idx++) 
  116. if (this.supportedLanguages.containsKey((language = st.nextToken()))) 
  117. this.language = this.parseLocale(language); 
  118. /* 
  119. * Hilfsfunktion fr setLanguage(). 
  120. */ 
  121. private String parseLocale(String language) 
  122. StringTokenizer st = new StringTokenizer(language, "-"); 
  123. if (st.countTokens() == 2) 
  124. return st.nextToken(); 
  125. else 
  126. return language; 
  127. /** 
  128. * Liefert das L?nderkürzel der vom Benutzer 
  129. * bevorzugten Sprache. 
  130. */ 
  131. public String getLanguage() 
  132. return this.language; 
  133. private void setLocale() 
  134. this.locale = new Locale(this.language, ""); 
  135. /** 
  136. * Liefert ein Locale-Objekt mit der Sprach-Prferenz des verwendeten Browsers 
  137. */ 
  138. public Locale getLocale() 
  139. return this.locale;