* GET /day14/demo1?name=zhangsan HTTP/1.1 * 方法: 1. 获取请求方式 :GET * String getMethod() 2. (*)获取虚拟目录:/day14 * String getContextPath() 3. 获取Servlet路径: /demo1 * String getServletPath() 4. 获取get方式请求参数:name=zhangsan * String getQueryString() 5. (*)获取请求URI:/day14/demo1 * String getRequestURI(): /day14/demo1 * StringBuffer getRequestURL() :http://localhost/day14/demo1 * URL:统一资源定位符 : http://localhost/day14/demo1 中华人民共和国 * URI:统一资源标识符 : /day14/demo1 共和国 6. 获取协议及版本:HTTP/1.1 * String getProtocol() 7. 获取客户机的IP地址: * String getRemoteAddr()
package cn.itcast.web.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
*/
@WebServlet("/requestDemo1")
public class RequestDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* 1. 获取请求方式 :GET * String getMethod() 2. (*)获取虚拟目录:/day14 * String getContextPath() 3. 获取Servlet路径: /requestDemo1 * String getServletPath() 4. 获取get方式请求参数:name=zhangsan * String getQueryString() 5. (*)获取请求URI:/day14/demo1 * String getRequestURI(): /day14/requestDemo1 * StringBuffer getRequestURL() :http://localhost/day14/requestDemo1 6. 获取协议及版本:HTTP/1.1 * String getProtocol() 7. 获取客户机的IP地址: * String getRemoteAddr() */ //1. 获取请求方式 :GET String method = request.getMethod(); System.out.println(method); //2.(*)获取虚拟目录:/day14 String contextPath = request.getContextPath(); System.out.println(contextPath); //3. 获取Servlet路径: /demo1 String servletPath = request.getServletPath(); System.out.println(servletPath); //4. 获取get方式请求参数:name=zhangsan String queryString = request.getQueryString(); System.out.println(queryString); //5.(*)获取请求URI:/day14/demo1 String requestURI = request.getRequestURI(); StringBuffer requestURL = request.getRequestURL(); System.out.println(requestURI); System.out.println(requestURL); //6. 获取协议及版本:HTTP/1.1 String protocol = request.getProtocol(); System.out.println(protocol); //7. 获取客户机的IP地址: String remoteAddr = request.getRemoteAddr(); System.out.println(remoteAddr); }
}
* 方法: * (*)String getHeader(String name):通过请求头的名称获取请求头的值 * Enumeration<String> getHeaderNames():获取所有的请求头名称
* 请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数 * 步骤: 1. 获取流对象 * BufferedReader getReader():获取字符输入流,只能操作字符数据 * ServletInputStream getInputStream():获取字节输入流,可以操作所有类型数据 * 在文件上传知识点后讲解 2. 再从流对象中拿数据