获取请求数据
获取请求行数据:
GET /web/requestInit?name=zhangsan&name=lisi HTTP/1.1
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1. 获取请求方式 :GET String getMethod() String get = req.getMethod(); System.out.println(get);//GET //2. (*)获取虚拟目录:/web String getContextPath() String path = req.getContextPath(); System.out.println(path);// /web //3. 获取Servlet路径: /requestInit String getServletPath() String servletPath = req.getServletPath(); System.out.println(servletPath);// /requestInit //4. 获取get方式请求参数:name=zhangsan String getQueryString() String queryString = req.getQueryString(); System.out.println(queryString);//name=zhangsan&name=lisi //5. (*)获取请求URI:/web/requestInit String getRequestURI() StringBuffer getRequestURL() String uri = req.getRequestURI(); System.out.println(uri);///web/requestInit StringBuffer url = req.getRequestURL(); System.out.println(url);//http://127.0.0.1:8080/web/requestInit //6. 获取协议及版本:HTTP/1.1 String getProtocol() String protocol = req.getProtocol(); System.out.println(protocol);//HTTP/1.1 //7. 获取客户机的IP地址: String getRemoteAddr() String addr = req.getRemoteAddr(); System.out.println(addr);//127.0.0.1 }
获取请求头数据:
Host: localhost:8080
/** * 获取请求头信息 * 1.根据请求头名称获取内容 String getHeader(String headerName) * 2.获取所有的请求头名称 Enumeration<String> getHeaderNames():获取所有的请求头名称 */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.根据请求头名称获取内容 String getHeader(String headerName) String agent = req.getHeader("user-agent"); System.out.println(agent); //2.获取所有的请求头名称 Enumeration<String> getHeaderNames():获取所有的请求头名称 //Enumeration 古老的那个迭代器,现在被Iterator代替了 Enumeration<String> headerNames = req.getHeaderNames(); while (headerNames.hasMoreElements()){ String name = headerNames.nextElement(); String header = req.getHeader(name); System.out.println(name + "--->" + header); } }
获取请求体数据:只有POST请求方式才有请求体,在请求体中封装了POST请求的请求参数
获取字符输入流 BufferedReader getReader() : 获取字符输入流,只能操作字符数据
/** * 获取请求体数据 * 1.获取字符输入流 BufferedReader getReader() : 获取字符输入流,只能操作字符数据 * 2.获取字节输入流 ServletInputStream getInputStream() : 获取字节输入流,可以操作所有类型数据 */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { BufferedReader reader = req.getReader(); //关于I/O流的操作,方式一: System.out.println("方式一****************"); char[] cBuff = new char[1024]; int len; while ((len = reader.read(cBuff)) != -1){ for (int i = 0; i < len; i++) { System.out.print(cBuff[i]);//username=superbody&password=123456 } } // System.out.println("方式二****************"); // while ((len = reader.read(cBuff)) != -1){ // String str = new String(cBuff,0,len); // System.out.println(str);//username=superbody&password=123456 // } // System.out.println("方式三****************"); // String line = null; // while ((line = reader.readLine()) != null){ // System.out.println(line);//username=superbody&password=123456 // } }
获取字节输入流 ServletInputStream getInputStream() : 获取字节输入流,可以操作所有类型数据
其他功能:
获取请求参数通用的方式:不区分POST请求和GET请求
String getParameter(String name) : 根据参数名称获取参数值 name=lisi&password=1234
String[] getParameterValues(String name) : 根据参数名称获取参数值的数组 hobby=game&hobby=study
Enumeration getParameterNames() : 获取所有请求的参数名称
Map<String,String[]> getParameterMap() : 获取所有参数的map集合
/** * 获取请求参数的通用方法 */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.String getParameter(String name) : 根据参数名称获取参数值 name=lisi&password=1234 System.out.println("********getParameter**********"); String username = req.getParameter("username"); String password = req.getParameter("password"); System.out.println("username=" + username + ",password=" + password); //2.String[] getParameterValues(String name) : 根据参数名称获取参数值的数组 hobby=game&hobby=study System.out.println("********getParameterValues**********"); String[] hobbies = req.getParameterValues("hobby"); for (String hobby : hobbies) { System.out.println("hobby=" + hobby); } //3.Enumeration<String> getParameterNames() : 获取所有请求的参数名称 System.out.println("********getParameterNames**********"); Enumeration<String> names = req.getParameterNames(); while (names.hasMoreElements()){ String name = names.nextElement(); String value = req.getParameter(name); System.out.println("name=" + name + ",value=" + value); } //4.Map<String,String[]> getParameterMap() : 获取所有参数的map集合 System.out.println("********getParameterMap**********"); Map<String, String[]> map = req.getParameterMap(); //操作map的第一种方式 System.out.println("方式一"); Set<String> key = map.keySet(); for (String name : key) { String[] values = map.get(name); for (String value : values) { System.out.println("name=" + name + ",value=" + value); } } //操作map的第二种方式 System.out.println("方式二"); Set<Map.Entry<String, String[]>> entries = map.entrySet(); for (Map.Entry<String, String[]> entry : entries) { String entryKey = entry.getKey(); String[] entryValues = entry.getValue(); for (String value : entryValues) { System.out.println("entryKey=" + entryKey + ",entryValue=" + value); } } }
POST请求中文乱码问题:
req.setCharacterEncoding("UTF-8");//在获取请求参数前,设置编码为UTF-8
请求转发:一种在服务器内部资源跳转的方式
步骤:
通过Request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)
使用ReqestDispatcher对象进行转发 forward(ServletRequest req,ServletResponse resp)
req.getRequestDispatcher("/RequestForward").forward(req,resp);//链式编程
请求转发的特点:
共享数据:Request域
获取ServletContext
作用:用来简化javaBean的封装
常用方法:
导入:
<dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.4</version> </dependency>