request
·····封装了客户端所有的请求数据!
·····请求协议中的数据都可以通过request对象来获取!
------------------------------------
reques的格式
---请求行
---请求头---空行
---请求体(GET没体)
------------------------------------
* 获取常用信息
·····获取客户端IP,···············request.getRemoteAddr()
·····请求方式,·····················request.getMethod()····· 结果:·POST/GET
* 获取HTTP请求头【服务端获取客户端的HTPP请求头】
··*****String getHeader(String name),适用于单值头
·· int getIntHeader(String name),适用于单值int类型的请求头
·· long getDateHeader(String name),适用于单值毫秒类型的请求头
·· Enumeration<String> getHeaders(String name),适用于多值请求头
·案例:
·> 通过User-Agent识别用户浏览器类型:User-Agent返回的值表示客户端的操作系统,浏览器版本等
String userAgent = request.getHeader("User-Agent");//获取名为User-Agent的请求头!
/ / Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36
----------------------------------------------------------------------------------------
String addr = request.getRemoteAddr();//获取客户端的IP地址
System.out.println("IP: " + addr);
System.out.println("请求方式:" + request.getMethod());//获取请求方式、Get/Post
----------------------------------------------------------------------------------------
* 获取请求URL
····························http://localhost:8080/day10_2/AServlet?username=xxx&password=yyy
·String getScheme():获取协议,http
·String getServerName():获取服务器名,localhost
· String getServerPort():获取服务器端口,8080
· *****String getContextPath():获取项目名,/day10_2··········上下文路径就是项目名
·String getServletPath():获取Servlet路径,/AServlet············Servlet的ulr-pattern
·String getQueryString():获取参数部分,即问号后面的部分。····查询参数路径········username=xxx&password=yyy
· String getRequestURI():获取请求URI,等于项目名+Servlet路径。·····URI···········/day10_2/AServlet
·String getRequestURL():获取请求URL,等于不包含参数的整个请求路径。http://localhost:8080/day10_2/AServlet
用一张图表示如下:
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------