⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 browserinfo.java

📁 JAVA编程百例书中各章节的所有例子的源代码,包括套接字编程
💻 JAVA
字号:
package ch04.section09;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class BrowserInfo
    extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html; charset=gb2312";
  protected HttpServletRequest request;
  protected HttpSession session;

  protected String userAgent;
  protected String company;
  protected String name;
  protected String os;

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    this.request = request;
    PrintWriter out = response.getWriter();
    this.request = request;
    this.session = request.getSession();
    MyInit();
    out.println("<html>");
    out.println("<head><title>TestServlet</title></head>");
    out.println("<body bgcolor=\"#ffffff\">");
    out.println("<p>你的服务提供商是:" + this.getCompany() + "</p>");
    out.println("<p>你的浏览器是:" + this.getName() + "</p>");
    out.println("<p>你的操作系统是:" + this.getOs() + "</p>");
    out.println("</body></html>");
  }

  public void MyInit() {
    this.setUserAgent(this.request.getHeader("User-Agent"));
    this.setCompany();
    this.setName();
    this.setOs();
  }

  public void setUserAgent(String httpUserAgent) {
    this.userAgent = httpUserAgent.toLowerCase();
  }

  public 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";
    }
  }

  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";
    }
  }

  public String getName() {
    return this.name;
  }

  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 2000") > -1 ||
          this.userAgent.indexOf("win2000") > -1) {
        this.os = "Windows 2000";
      }

      if (this.userAgent.indexOf("win16") > -1 ||
          this.userAgent.indexOf("windows 3.") > -1) {
        this.os = "Windows 3.x";
      }
    }
  }

  public String getOs() {
    return this.os;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -