-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestServlet.java
More file actions
144 lines (122 loc) · 5.95 KB
/
RequestServlet.java
File metadata and controls
144 lines (122 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.helloweenvsfei.servlet;
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;
import com.helloweenvsfei.util.IpUtil;
public class RequestServlet extends HttpServlet {
private static final long serialVersionUID = -7936817351382556277L;
/**
* @param accept
* @return 客户端浏览器接受的文件类型
*/
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(", $", "");
}
/**
* @param locale
* @return 语言环境名称
*/
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 "未知语言环境";
}
/**
* @param ip IP地址
* @return IP地址对应的物理位置
*/
private String getAddress(String ip){
return IpUtil.getIpAddress(ip);
}
/**
* @param userAgent
* @return 客户端浏览器信息
*/
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("Firefox") > 0) return "Firefox浏览器";
if(userAgent.indexOf("MSIE") > 0) return "IE 浏览器";
return "未知浏览器";
}
/**
* @param userAgent
* @return 客户端操作系统
*/
private String getOS(String userAgent) {
if(userAgent.indexOf("Windows NT 5.1") > 0) return "Windows XP";
if(userAgent.indexOf("Windows 98") > 0) return "Windows 98";
if(userAgent.indexOf("Windows NT 5.0") > 0) return "Windows 2000";
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();//本地AP即服务器AP
Locale locale = request.getLocale();//用户的语言环境
String localName = request.getLocalName();//服务器名
String contextPath = request.getContextPath();//context的路径
int localPort = request.getLocalPort();//服务器端口号
String method = request.getMethod();//GET还是POST
String pathInfo = request.getPathInfo();
String pathTranslated = request.getPathTranslated();
String protocol = request.getProtocol();//协议 HTTP协议
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();//客户端请求的URI
StringBuffer requestURL = request.getRequestURL();//客户端请求的URL
String scheme = request.getScheme();//协议头,这里为http
String serverName = request.getServerName();
int serverPort = request.getServerPort();
String servletPath = request.getServletPath();
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>");
// 这里<title></title>之间的信息在浏览器中显示为标题
out.println(" <HEAD><TITLE>Request Servlet</TITLE></HEAD>");
out.println(" <style>body, font, td, div {font-size:12px; line-height:18px; }</style>");
out.println(" <BODY>");
out.println("<b>您的IP为</b> " + remoteAddr + "<b>,位于</b> " + getAddress(remoteAddr) + "<b>;您使用</b> " + getOS(userAgent) + " <b>操作系统</b>," + 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> " + protocol + "。<b>URL协议头</b> " + 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>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}