?? browserinfo.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>你的服務(wù)提供商是:" + this.getCompany() + "</p>");
out.println("<p>你的瀏覽器是:" + this.getName() + "</p>");
out.println("<p>你的操作系統(tǒng)是:" + 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;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -