HttpServletRequest是ServletRequest接口的子接口,代表一个请求对象,用来封装所有从浏览器发送给服务器的数据.具体实现类是tomcat提供的
获取请求行相关的方法:
- 获取请求方式 :GET
String getMethod() - (*)获取虚拟目录:/request_demo
- String getContextPath()
- 获取Servlet路径: /demo1
String getServletPath() - 获取get方式请求参数:name=zhangsan
String getQueryString() - (*)获取请求URI:/request_demo/demo1
String getRequestURI(): /request_demo/demo1
StringBuffer getRequestURL() :http://localhost:8080/request_demo/demo1 - 获取协议及版本:HTTP/1.1
String getProtocol() - 获取客户机的IP地址:
String getRemoteAddr()
//请求行案例
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取请求方式
String method = request.getMethod();
System.out.println(method);
//2.获取虚拟目录
String contextPath = request.getContextPath();
System.out.println("虚拟目录:" + contextPath);
//3.获取servlet路径
String servletPath = request.getServletPath();
System.out.println("servlet路径:" + servletPath);
//4.获取请求的参数:name = zhangsan
String queryString = request.getQueryString();
System.out.println("参数:" + queryString);
//5.获取uri
String requestURI = request.getRequestURI();
System.out.println("uri==" + requestURI);
//6.获取url
StringBuffer requestURL = request.getRequestURL();
System.out.println("url==" + requestURL);
//7.获取http协议的版本
String protocol = request.getProtocol();
System.out.println("http版本:" + protocol);
//8.获取客户端端的ip地址
String remoteAddr = request.getRemoteAddr();
System.out.println("客户端ip:" + remoteAddr);
//uri:统一资源标识符,uri:项目名称+servlet路径---共和国
//url:统一资源定位符,uri:http://ip:8080/项目名称+servlet路径---中华人民共和国
//获取参数值
String name = request.getParameter("name");
System.out.println("name=="+name);
}