获取request的变量

由于IP代码未实现,先注释掉。

获取request的变量

 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; 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("BPM文件,");
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){
// return IpUtil.getIpAddress(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 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();
Locale locale = request.getLocale();
String localName = request.getLocalName();
String contextPath = request.getContextPath();
int localPort = request.getLocalPort();
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 requestedSessionId = 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 userPrincipal = 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("<!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> " + getOS(userAgent) + " <b>操作系统</b>," + getNavigator(userAgent) + " <b>。您使用</b> " + getLocale(locale) + "。<br/>");
out.println("<b>服务器IP为</b> " + 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();
}
}

RequestServlet.java

<?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>

web.xml

获取request的变量

获取request的变量

上一篇:PHP设计模式——责任链模式


下一篇:GraphLab:新的面向机器学习的并行框架