获取request的变量
由于IP代码未实现,先注释掉。
1 package com.helloweenvsfei.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.security.Principal; 6 import java.util.Locale; 7 8 import javax.servlet.ServletException; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 public class RequestServlet extends HttpServlet { 14 15 private String getAccept(String accept) { 16 StringBuffer buffer = new StringBuffer(); 17 if (accept.contains("image/gif")) 18 buffer.append("GIF文件,"); 19 if (accept.contains("image/x-xbitmap")) 20 buffer.append("BPM文件,"); 21 if (accept.contains("image/jpeg")) 22 buffer.append("JPG文件"); 23 if (accept.contains("application/vnd.ms-excel")) 24 buffer.append("Excel 文件, "); 25 if (accept.contains("application/vnd.ms-powerpoint")) 26 buffer.append("PPT 文件, "); 27 if (accept.contains("application/msword")) 28 buffer.append("Word 文件, "); 29 return buffer.toString().replaceAll(", $", ""); 30 } 31 32 private String getLocale(Locale locale) { 33 if (Locale.SIMPLIFIED_CHINESE.equals(locale)) 34 return "简体中文"; 35 if (Locale.TRADITIONAL_CHINESE.equals(locale)) 36 return "繁体中文"; 37 if (Locale.ENGLISH.equals(locale)) 38 return "英文"; 39 if (Locale.JAPANESE.equals(locale)) 40 return "日文"; 41 return "未知语言环境"; 42 } 43 44 // private String getAddress(String ip){ 45 // return IpUtil.getIpAddress(ip); 46 // } 47 48 private String getNavigator(String userAgent) { 49 if (userAgent.indexOf("TencentTraveler") > 0) 50 return "腾讯浏览器"; 51 if (userAgent.indexOf("Maxthon") > 0) 52 return "Maxthon浏览器"; 53 if (userAgent.indexOf("MyIE2") > 0) 54 return "MyIE2浏览器"; 55 if (userAgent.indexOf("Firefox") > 0) 56 return "Firefox浏览器"; 57 if (userAgent.indexOf("MSIE") > 0) 58 return "IE 浏览器"; 59 return "未知浏览器"; 60 } 61 private String getOS(String userAgent){ 62 if(userAgent.indexOf("Windows NT 5.1") > 0) return "Windows XP"; 63 if(userAgent.indexOf("Windows 98") > 0) return "Windows 98"; 64 if(userAgent.indexOf("Windows NT 5.0") > 0) return "Windows 2000"; 65 if(userAgent.indexOf("Linux") > 0) return "Linux"; 66 if(userAgent.indexOf("Unix") > 0) return "Unix"; 67 return "未知"; 68 } 69 public void doGet(HttpServletRequest request,HttpServletResponse response) 70 throws ServletException,IOException{ 71 request.setCharacterEncoding("UTF-8"); 72 response.setCharacterEncoding("UTF-8"); 73 74 response.setContentType("text/html"); 75 76 String authType = request.getAuthType(); 77 String localAddr = request.getLocalAddr(); 78 Locale locale = request.getLocale(); 79 String localName = request.getLocalName(); 80 String contextPath = request.getContextPath(); 81 int localPort = request.getLocalPort(); 82 String method = request.getMethod(); 83 String pathInfo = request.getPathInfo(); 84 String pathTranslated = request.getPathTranslated(); 85 String protocol = request.getProtocol(); 86 String queryString = request.getQueryString(); 87 String remoteAddr = request.getRemoteAddr(); 88 int port = request.getRemotePort(); 89 String remoteUser = request.getRemoteUser(); 90 String requestedSessionId = request.getRequestedSessionId(); 91 String requestURI = request.getRequestURI(); 92 StringBuffer requestURL = request.getRequestURL(); 93 String scheme = request.getScheme(); 94 String serverName = request.getServerName(); 95 int serverPort = request.getServerPort(); 96 String servletPath = request.getServletPath(); 97 Principal userPrincipal = request.getUserPrincipal(); 98 99 String accept = request.getHeader("accept"); 100 String referer = request.getHeader("referer"); 101 String userAgent = request.getHeader("user-agent"); 102 103 String serverInfo = this.getServletContext().getServerInfo(); 104 105 PrintWriter out = response.getWriter(); 106 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 107 out.println("<HTML>"); 108 109 // 这里<title></title>之间的信息在浏览器中显示为标题 110 out.println(" <HEAD><TITLE>Request Servlet</TITLE></HEAD>"); 111 out.println(" <style>body, font, td, div {font-size:12px; line-height:18px; }</style>"); 112 out.println(" <BODY>"); 113 114 out.println("<b>您的IP为</b> " + remoteAddr + "<b>;您使用</b> " + getOS(userAgent) + " <b>操作系统</b>," + getNavigator(userAgent) + " <b>。您使用</b> " + getLocale(locale) + "。<br/>"); 115 out.println("<b>服务器IP为</b> " + localAddr + "<b>;服务器使用</b> " + serverPort + " <b>端口,您的浏览器使用了</b> " + port + " <b>端口访问本网页。</b><br/>"); 116 out.println("<b>服务器软件为</b>:" + serverInfo + "。<b>服务器名称为</b> " + localName + "。<br/>"); 117 out.println("<b>您的浏览器接受</b> " + getAccept(accept) + "。<br/>"); 118 out.println("<b>您从</b> " + referer + " <b>访问到该页面。</b><br/>"); 119 out.println("<b>使用的协议为</b> " + protocol + "。<b>URL协议头</b> " + scheme + ",<b>服务器名称</b> " + serverName + ",<b>您访问的URI为</b> " + requestURI + "。<br/>" ); 120 out.println("<b>该 Servlet 路径为</b> " + servletPath + ",<b>该 Servlet 类名为</b> " + this.getClass().getName() + "。<br/>"); 121 out.println("<b>本应用程序在硬盘的根目录为</b> " + this.getServletContext().getRealPath("") + ",<b>网络相对路径为</b> " + contextPath + "。 <br/>"); 122 123 out.println("<br/>"); 124 125 out.println("<br/><br/><a href=" + requestURI + "> 点击刷新本页面 </a>"); 126 127 out.println(" </BODY>"); 128 out.println("</HTML>"); 129 out.flush(); 130 out.close(); 131 } 132 }
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>com.helloweenvsfei.servlet.FirstServlet</servlet-class> <init-param> <param-name>message</param-name> <param-value>welcome to FirstServlet</param-value> </init-param> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>RequestServlet</servlet-name> <servlet-class>com.helloweenvsfei.servlet.RequestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/servlet/FirstServlet</url-pattern> <url-pattern>/servlet/FirstServlet.asp</url-pattern> <url-pattern>/servlet/FirstServlet.php</url-pattern> <url-pattern>/servlet/FirstServlet.jsp</url-pattern> <url-pattern>/servlet/FirstServlet.aspx</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>RequestServlet</servlet-name> <url-pattern>/servlet/RequestServlet</url-pattern> </servlet-mapping> </web-app>