Http请求和HttpServletRequest中获得对应参数的方

来源:互联网 发布:nba战报今日战报数据 编辑:程序博客网 时间:2024/06/06 20:06

http://blog.csdn.net/oncealong/article/details/51383563

Http请求

GET /firstSample/hello HTTP/1.1    请求行Host: localhost:8080               请求头Connection: keep-aliveCache-Control: max-age=0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36DNT: 1Accept-Encoding: gzip, deflate, sdchAccept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.6Cookie:                                    空行name=oncealong&password=123456   (可选)实体内容,在post请求时用于提交
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

一个Http请求可以分为请求行, 请求头, 和可选的实体内容, 我们分别来看下http和HttpServletRequest中的方法如何对应.

请求行

GET /firstSample/hello HTTP/1.1

GET: 请求方式, 其中常见的额有GET 、 POST、 HEAD、 TRACE、 PUT、 CONNECT 、DELETE 
/firstSample/hello : 可以使用url和uri. url只能用于网络 
HTTP/1.1: 协议版本. 之前还有http/1.0, 在一次连接中只能发送一次请求. 现在都是使用http1.1, 可以在一次连接中发送多次请求.

HttpServletRequest中用于获取请求行的方法:

request.getMethod();     //请求方式request.getRequetURI();  //request.getRequetURL()   请求资源request.getProtocol();   //请求http协议版本
  • 1
  • 2
  • 3

请求头

Host: localhost:8080 (必须的)当前请求访问的目标地址(主机:端口) 
Connection: keep-alive 浏览器跟服务器连接状态。close: 连接关闭 keep-alive:保存连接。 
Cache-Control: max-age=0 在0秒内不会重新访问服务器,也就是页面立即失效。 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8 
Upgrade-Insecure-Requests: 1 
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36 
DNT: 1 
Accept-Encoding: gzip, deflate, sdch 浏览器接受的数据压缩格式 
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.6 浏览器接受的语言 
Cookie: 浏览器保存的cookie信息

HttpServletRequest中用于获取请求头的方法:

request.getHeader("name")   //根据name,获取对应请求头数据request.getHeaderNames()    //获取所有的请求头名称
  • 1
  • 2
  • 3

实体内容

name=oncealong&password=123456 
实体内容,在post请求时参数放在实体内容中

HttpServletRequest中用于获取请求实体内容的方法:

request.getInputStream()   //获取实体内容数据
  • 1

对于get方式, 要获取提交的参数需要使用request.getQueryString();方法,这样造成API的不一致, 很麻烦. 所以最好使用如下统一方便的获取参数的方式:

request.getParameter("参数名");  //根据参数名获取参数值(注意,只能获取一个值的参数)request.getParameterValue("参数名“); //根据参数名获取参数值(可以获取多个值的参数)request.getParameterNames();   //获取所有参数名称列表  
  • 1
  • 2
  • 3
举例:

假设客户端请求的地址:http://localhost:8082/TestReq/MyServlet/username=李雷&age=20

request.getRequestURL http://localhost:8082/TestReq/MyServlet ---客户请求求的URL,不包括参数数据

request.getRequestURI: /TestReq/MyServlet ---将URL的域名和尾随的参数截取掉,剩下的那部分就是URI

request.getContextPath: /TestReq ---即斜杆加工程名

request.getRealPath("/WEB-INF"): D:\omc_jboss\server\default\.\deploy\TestReq.war\WEB-INF ---工程部署的完整路径字符串接上参数中的字符串

request.getMethod: GET ---HTTP请求的的方法名,默认是GET,也可以指定PUT或POST

request.getAuthType: null ---返回包含用来保护servlet身份验证方案的名称,如BASIC和SSL,如果是null表示未不受保护

request.getProtocol: HTTP/1.1 ---返回请求的协议名和版本,如HTTP/1.1等

request.getScheme: http ---返回请求的方案名,如http,ftp,https等

request.getServletPath: /MyServlet ---工程之后到参数之前的这部分字符串

request.getPathInfo: null ---字符串包含与客户端发送请求的URL相关的额外信息

request.getContentLength: -1 ---请求体内容的长度,只对POST和PUT类型的请求有效

request.getContentType: null ---请求体内容类型

request.getServerName: localhost ---服务器主机名

request.getServerPort: 8082 ---服务器上web应用的访问端口

request.getRemoteAddr: 127.0.0.1 ---发送请求的客户端主机的IP

request.getRemoteHost: 127.0.0.1 ---发送请求的客户端主机名,如果不确定返回的是IP

request.getRemoteUser: null ---如果对发送请求的客户端主机用户进行了身份验证,则返回登录信息,否则返回null

request.getPathTranslated: null ---返回一个表示在服务器文件系统上的PathInfol转换成路径的字符串

request.getQueryString: username=李雷&age=20 ---返回URL上的参数部分的字符串,必须是GET的请求才有效,不然报错

这里的URL参数中带有中文,是通过字符转码的:String eQuery=new String(request.getQueryString().getBytes("ISO-8859-1"))

response的响应内容:response.setContentType("text/html;charset=gbk"),才可以正常显示页面中文

阅读全文
0 0