获取request的变量
由于IP代码未实现,先注释掉。
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的变量的更多相关文章
- Servlet获取request的变量方法.
1.String localAddr = request.getLocalAddr(); // 获取本地IP,即服务器 IP 2.setAttribute(String name,Object ...
- Struts2获取request三种方法
Struts2获取request三种方法 struts2里面有三种方法可以获取request,最好使用ServletRequestAware接口通过IOC机制注入Request对象. 在Actio ...
- spring mvc controller中获取request head内容
spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...
- 分享知识-快乐自己:Struts2中 获取 Request和Session
目录结构: POM: <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEnco ...
- Struts2获取request的几种方式汇总
Struts2获取request三种方法 struts2里面有三种方法可以获取request,最好使用ServletRequestAware接口通过IOC机制注入Request对象. 在Action中 ...
- jsp页面 如何通过el表达式获取request属性值
1. 我在一个超连接后加个参数如: http://localhost:8080/test/testjstl.jsp?pid=001 此时在jsp页面中,获取jsp传过来的pid的参数值 ...
- 在SpringMVC中获取request对象
1.注解法 @Autowired private HttpServletRequest request; 2. 在web.xml中配置一个监听 <listener> <listen ...
- Path形状获取字符串型变量数据
Path形状获取字符串型变量数据: var path = new Path(); path.Data = Geometry.Parse("M 100,200 C 100,25 400,350 ...
- 在SpringMVC中获取request对象的几种方式
1.最简单的方式(注解法) @Autowired private HttpServletRequest request; 2.最麻烦的方法 a. 在web.xml中配置一个监听 <listene ...
随机推荐
- Python 基本类型转换
python 有关字符串处理有哪些好用的方法?reverse len 字符串分割,合并?截取?查找? find index join split unicode字符串的表示 ""& ...
- Linux上Eclipse项目右键菜单没有Maven
在Centos 7上安装了eclipse以后,着实很兴奋.eclipse luna版本自带maven.但是用mvn eclipse:eclipse创建的java工程,在右键菜单居然没有Maven按钮, ...
- python 邮件发送 脚本
import smtplib from email.header import Header from email.mime.text import MIMEText from_addr = 'XXX ...
- 《TCP/IP 详解 卷一》读书笔记-----Ping&Traceroute
1.ping是用于测试对方主机是否可达的命令,其实本质上就是echo类型的ICMP报文.同时,ping还能用于计算RTT(round-trip time),即两台主机间的往返时延. 2.随着网络安全意 ...
- 广度优先搜索 cdoevs 1226 倒水问题
cdoevs 1226 倒水问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升 ...
- HDU 1878 欧拉回路
并查集水题. 一个图存在欧拉回路的判断条件: 无向图存在欧拉回路的充要条件 一个无向图存在欧拉回路,当且仅当该图所有顶点度数都是偶数且该图是连通图. 有向图存在欧拉回路的充要条件 一个有向图存在欧拉回 ...
- 第2章 面向对象的设计原则(SOLID):1_单一职责原则(SRP)
1. 单一职责原则(Single Responsibility Principle,SRP) 1.1 单一职责的定义 (1)定义:一个类应该仅有一个引起它变化的原因.这里变化的原因就是所说的“职责”. ...
- python算法:rangeBitwiseAnd(连续整数的与)
def rangeBitwiseAnd(self, m, n): i = 0 while m != n: m >>= 1 n >>= 1 i += 1 return n < ...
- 【转】【C#】C#性能优化总结
1. C#语言方面 1.1 垃圾回收 垃圾回收解放了手工管理对象的工作,提高了程序的健壮性,但副作用就是程序代码可能对于对象创建变得随意. 1.1.1 避免不必要的对象创 ...
- [转]仿World Wind构造自己的C#版插件框架——WW插件机制精简改造
很久没自己写东西啦,早该好好总结一下啦!一个大师说过“一个问题不应该被解决两次!”,除了一个好脑筋,再就是要坚持总结. 最近需要搞个系统的插件式框架,我参照World Wind的插件方式构建了个插件框 ...