一、get类方法
通过实际输出验证方法:
请求路径http://10.20.20.184:8080/firstssm/test
请求主机地址:10.20.20.169
前端代码:
<body> <h1>Hello</h1> <form action="test" method="post"> <input name="test" value="测试"> <input id="sayHello" type="submit" value="say Hello"> </form> </body>
后台代码:
@RequestMapping("test") @ResponseBody public String test(HttpServletRequest request) { JSONObject json = new JSONObject(); json.put("remoteHost", request.getRemoteHost()); json.put("remoteAddr", request.getRemoteAddr()); json.put("remotePort", request.getRemotePort()); json.put("remoteUser", request.getRemoteUser()); json.put("localPort", request.getLocalPort()); json.put("localAddr", request.getLocalAddr()); json.put("localName", request.getLocalName()); json.put("requestURL", request.getRequestURL()); json.put("contentLength", request.getContentLength()); json.put("contextPath", request.getContextPath()); json.put("servletPath", request.getServletPath()); json.put("realPath", request.getRealPath("test")); json.put("CharacterEncoding",request.getCharacterEncoding()); json.put("serverPort", request.getServerPort()); json.put("authType", request.getAuthType()); json.put("contentType", request.getContentType()); json.put("pathInfo", request.getPathInfo()); json.put("pathTranslated", request.getPathTranslated()); json.put("protocol", request.getProtocol()); json.put("queryString", request.getQueryString()); json.put("scheme", request.getScheme()); json.put("serverName", request.getServerName()); logger.info(json.toString()); return json.toString(); }
输出结果:
{ "localName": "DBGD16C01005018.XXXXX.com",//服务器主机名 "localAddr": "10.20.20.184",//服务器地址 "localPort": 8080,//服务器接收请求端口
"remoteHost": "10.20.20.169",//发送请求的主机ip "remotePort": 54898,//发送请求的端口 "remoteAddr": "10.20.20.169",//发送请求的地址
"serverName": "10.20.20.184",//服务器域名,没有域名使用ip "serverPort": 8080,//服务器接收请求端口
"protocol": "HTTP/1.1",//协议名称 "scheme": "http",//使用的那种协议
"contextPath": "/firstssm",//项目名 "servletPath": "/test",//项目名后的路径,servlet的请求路径 "requestURL": "http://10.20.20.184:8080/firstssm/test",//浏览器的请求路径
"CharacterEncoding": "UTF-8",//对浏览器请求信息的解码的编码,可通过过setCharacterEncoding设置 "contentLength": 23,//内容区长度 "realPath": "D:\java\apache-tomcat-8.5.37\webapps\firstssm\test",//参数是test "contentType": "application/x-www-form-urlencoded",//内容区的请求方式。
"queryString":"a=a&b=b",//get请求的参数
}