项目名:ServletII
Servlet类名:RequestServlet.java 和 IpUtil.java
RequestServlet.java:
package com.helloweenvsfei.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;
import java.util.Locale;
import com.helloweenvsfei.util.IpUtil;
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().replace(", $", "");
}
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) {
return IpUtil.getHostName(ip);
}
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 "未知浏览器";
}
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 2000") > 0)
return "Windows 2000";
if (userAgent.indexOf("Linux") > 0)
return "Linux";
if (userAgent.indexOf("Unix") > 0)
return "Unix";
return "未知";
}
/**
* Constructor of the object.
*/
public RequestServlet() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
String autoType = request.getAuthType();
String localAddr = request.getLocalAddr();
String localName = request.getLocalName();
int localPort = request.getLocalPort();
Locale locale = request.getLocale();
String contextpath = request.getContextPath();
String method = request.getMethod();
String pathinfo = request.getPathInfo();
String pathtranslated = request.getPathTranslated();
String protocol = request.getProtocol();
String querystring = request.getQueryString();
String remoteaddr = request.getRemoteAddr();
int port = request.getRemotePort();
String remoteuser = request.getRemoteUser();
String requestsessionid = request.getRequestedSessionId();
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
String scheme = request.getScheme();
String servername = request.getServerName();
int serverport = request.getServerPort();
String servletpath = request.getServletPath();
Principal principal = request.getUserPrincipal();
String accept = request.getHeader("accept");
String referer = request.getHeader("referer");
String userAgent = request.getHeader("user-agent");
String serverInfo = this.getServletContext().getServerInfo();
PrintWriter out = response.getWriter();
out
.println("");
out.println("");
out.println("
package com.helloweenvsfei.util;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IpUtil {
/**
* 非法IP地址常量。
*
* @since 0.12
*/
public static final String INVALID_IP = "0.0.0.0";
/**
* 未知主机名常量。
*
* @since 0.12
*/
public static final String UNKNOWN_HOST = "";
/**
* 私有构造方法,防止类的实例化,因为工具类不需要实例化。
* @return
*/
private void IPUtil() {
}
/**
* 根据主机名得到IP地址字符串。
*
* @param hostName
* 要查找地址的主机名
* @return 对应主机的IP地址,主机名未知或者非法时返回INVALID_IP。
*/
public static String getByName(String hostName) {
try {
InetAddress inet = InetAddress.getByName(hostName);
return inet.getHostAddress();
} catch (UnknownHostException e) {
return INVALID_IP;
}
}
/**
* 根据IP地址得到主机名。
*
* @param ip
* 要查找主界面的IP地址
* @return 对应IP的主机名,IP地址未知时返回UNKNOWN_HOST,IP地址未知也可能是网络问题造成的。
*/
public static String getHostName(String ip) {
try {
InetAddress inet = InetAddress.getByName(ip);
return inet.getHostName();
} catch (UnknownHostException e) {
return UNKNOWN_HOST;
}
}
}
Web.xml: