package com.ch02.firstServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RequestServlet extends HttpServlet {
private String getAccept(String accept){//返回接受的文件类型
StringBuffer buffer = new StringBuffer();
if(accept.contains("image/gif")) buffer.append("GIF 文件,");
if(accept.contains("image/x-xbitmap")) buffer.append("BMP 文件,");
if(accept.contains("image/jpeg")) buffer.append("JPG 文件,");
if(accept.contains("application/vnd.ms-excel")) buffer.append("excel 文件,");
if(accept.contains("application/vnd.ms-powerpoint")) buffer.append("PPT 文件,");
if(accept.contains("application/msword")) buffer.append("Word 文件,");
return buffer.toString().replaceAll(",", "");
}
private String getLocale(Locale locale){//返回客户端语言环境名称
if(Locale.SIMPLIFIED_CHINESE.equals(locale)) return "简体中文";
if(Locale.TRADITIONAL_CHINESE.equals(locale)) return "繁体中文";
if(Locale.ENGLISH.equals(locale)) return "英文";
if(Locale.JAPANESE.equals(locale)) return "日文";
return "未知语言";
}
private String getAddress(String ip){//返回ip地址对应的
return null;
}
private String getNavigator(String userAgent){
if(userAgent.indexOf("TencentTraveler")>0) return "腾讯浏览器";
if(userAgent.indexOf("Maxthon")>0) return "Maxthon 浏览器";
if(userAgent.indexOf("MyIE2")>0) return "MyIE2浏览器";
if(userAgent.indexOf("MSIE")>0) return "IE浏览器";
return "未知浏览器";
}
private String getOS(String userAgent){
if(userAgent.indexOf("Window NT 5.1")>0) return "Window XP";
if(userAgent.indexOf("Linux")>0) return "Linux";
if(userAgent.indexOf("Unix")>0) return "Unix";
return "未知";
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
String authType = request.getAuthType();
String localAddr = request.getLocalAddr();//本机ip
String localName = request.getLocalName();//本机服务器名
int localPort = request.getLocalPort();
Locale locale = request.getLocale();//本地语言环境
String contextPath = request.getContextPath();//context路径
String method = request.getMethod();
String pathInfo = request.getPathInfo();
String pathTranstlated = request.getPathTranslated();
String protocal = request.getProtocol();//查询协议
String queryString = request.getQueryString();//查询字符串
String remoteAddr = request.getRemoteAddr();//远程ip
int port =request.getRemotePort();
String remoteUser = request.getRemoteUser();//远程用户
String requestedSessionId = request.getRequestedSessionId();//客户端session id
String requestUri = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();//远程URL
String scheme = request.getScheme();//协议头 http
String serverName = request.getServerName();//服务器名
int serverPort = request.getServerPort();
String servletPath = request.getServletPath();;//servlet的路径
Principal userPrincipal = request.getUserPrincipal();
String accept = request.getHeader("accept");
String referer = request.getHeader("referer");
String userAgent = request.getHeader("user-agent");//User Agent 信息 包括操作系统类型及版本、浏览器信息机版本号
String serverInfo = this.getServletContext().getServerInfo();
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<b>你的ip为</b>"+remoteAddr+"<b>,位于</b>"+getAddress(remoteAddr)+"<b>你使用</b>"+getOS(userAgent)+"<b>操作系统</b>");
out.println(getNavigator(userAgent)+"<b>。你使用</b>"+getLocale(locale)+"。<br/>");
out.println("<b>服务器ip为</b>"+localAddr+"<b>,位于</b>"+getAddress(localAddr)+"<b>;服务器使用</b>"+serverPort+"<b>端口,你的浏览器使用了</b>"+port+"<b>端口访问本页面.</b><br/>");
out.println("<b>服务器软件为</b>:"+serverInfo+"。<b>服务器名称为</b>"+localName+"。<br/>");
out.println("<b>你的浏览器接受</b>"+getAccept(accept)+"。<br/>");
out.println("<b>你从</b>"+referer+"<b>访问的到该页面。</b><br/>");
out.println("<b>使用的协议为</b>"+protocal+"。<b>URL协议头</b>");
out.println(scheme+",<b>服务器名称</b>"+serverName+",<b>你访问的uri为</b>"+requestUri+"。<br/>");
out.println("<b>该servlet路径</b>"+servletPath+",<b>该servlet类名为</b>"+this.getClass().getName()+"。<br/>");
out.println("<b>本应用程序硬盘的根目录为</b>"+this.getServletContext().getRealPath("")+",<b>网络相对路径为</b>"+contextPath+"。<br/>");
out.println("<br/>");
out.println("<br/><br/><a href="+requestUri+">单次刷新本页</a>");
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RequestServlet extends HttpServlet {
private String getAccept(String accept){//返回接受的文件类型
StringBuffer buffer = new StringBuffer();
if(accept.contains("image/gif")) buffer.append("GIF 文件,");
if(accept.contains("image/x-xbitmap")) buffer.append("BMP 文件,");
if(accept.contains("image/jpeg")) buffer.append("JPG 文件,");
if(accept.contains("application/vnd.ms-excel")) buffer.append("excel 文件,");
if(accept.contains("application/vnd.ms-powerpoint")) buffer.append("PPT 文件,");
if(accept.contains("application/msword")) buffer.append("Word 文件,");
return buffer.toString().replaceAll(",", "");
}
private String getLocale(Locale locale){//返回客户端语言环境名称
if(Locale.SIMPLIFIED_CHINESE.equals(locale)) return "简体中文";
if(Locale.TRADITIONAL_CHINESE.equals(locale)) return "繁体中文";
if(Locale.ENGLISH.equals(locale)) return "英文";
if(Locale.JAPANESE.equals(locale)) return "日文";
return "未知语言";
}
private String getAddress(String ip){//返回ip地址对应的
return null;
}
private String getNavigator(String userAgent){
if(userAgent.indexOf("TencentTraveler")>0) return "腾讯浏览器";
if(userAgent.indexOf("Maxthon")>0) return "Maxthon 浏览器";
if(userAgent.indexOf("MyIE2")>0) return "MyIE2浏览器";
if(userAgent.indexOf("MSIE")>0) return "IE浏览器";
return "未知浏览器";
}
private String getOS(String userAgent){
if(userAgent.indexOf("Window NT 5.1")>0) return "Window XP";
if(userAgent.indexOf("Linux")>0) return "Linux";
if(userAgent.indexOf("Unix")>0) return "Unix";
return "未知";
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
String authType = request.getAuthType();
String localAddr = request.getLocalAddr();//本机ip
String localName = request.getLocalName();//本机服务器名
int localPort = request.getLocalPort();
Locale locale = request.getLocale();//本地语言环境
String contextPath = request.getContextPath();//context路径
String method = request.getMethod();
String pathInfo = request.getPathInfo();
String pathTranstlated = request.getPathTranslated();
String protocal = request.getProtocol();//查询协议
String queryString = request.getQueryString();//查询字符串
String remoteAddr = request.getRemoteAddr();//远程ip
int port =request.getRemotePort();
String remoteUser = request.getRemoteUser();//远程用户
String requestedSessionId = request.getRequestedSessionId();//客户端session id
String requestUri = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();//远程URL
String scheme = request.getScheme();//协议头 http
String serverName = request.getServerName();//服务器名
int serverPort = request.getServerPort();
String servletPath = request.getServletPath();;//servlet的路径
Principal userPrincipal = request.getUserPrincipal();
String accept = request.getHeader("accept");
String referer = request.getHeader("referer");
String userAgent = request.getHeader("user-agent");//User Agent 信息 包括操作系统类型及版本、浏览器信息机版本号
String serverInfo = this.getServletContext().getServerInfo();
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<b>你的ip为</b>"+remoteAddr+"<b>,位于</b>"+getAddress(remoteAddr)+"<b>你使用</b>"+getOS(userAgent)+"<b>操作系统</b>");
out.println(getNavigator(userAgent)+"<b>。你使用</b>"+getLocale(locale)+"。<br/>");
out.println("<b>服务器ip为</b>"+localAddr+"<b>,位于</b>"+getAddress(localAddr)+"<b>;服务器使用</b>"+serverPort+"<b>端口,你的浏览器使用了</b>"+port+"<b>端口访问本页面.</b><br/>");
out.println("<b>服务器软件为</b>:"+serverInfo+"。<b>服务器名称为</b>"+localName+"。<br/>");
out.println("<b>你的浏览器接受</b>"+getAccept(accept)+"。<br/>");
out.println("<b>你从</b>"+referer+"<b>访问的到该页面。</b><br/>");
out.println("<b>使用的协议为</b>"+protocal+"。<b>URL协议头</b>");
out.println(scheme+",<b>服务器名称</b>"+serverName+",<b>你访问的uri为</b>"+requestUri+"。<br/>");
out.println("<b>该servlet路径</b>"+servletPath+",<b>该servlet类名为</b>"+this.getClass().getName()+"。<br/>");
out.println("<b>本应用程序硬盘的根目录为</b>"+this.getServletContext().getRealPath("")+",<b>网络相对路径为</b>"+contextPath+"。<br/>");
out.println("<br/>");
out.println("<br/><br/><a href="+requestUri+">单次刷新本页</a>");
}
}