A data acquisition request message Request


More detailed description of the method of obtaining the request object data request message, although the introduction a lot, but generally the general procedure used in the actual development getParameter(String name)to pass over the layer obtained data view. I heard that Map<String,String[]> getParameterMap()is also very common, but that she has not used in the project.

An acquisition request line

(1) a method of obtaining request line

The method of obtaining the request line Explanation
String getMethod() Get request method
☆String getContextPath() 获得虚拟路径
☆String getServletPath() 获得Servlet路径
String getQueryString() Way to get get request parameters
☆String getRequestURI() 获得请求URI
☆String getRequestURL() 获得请求URL
String getProtocol() Obtain agreement and release
String getRemoteAddr() Obtain the IP address of the client

The request line from left to right, respectively 请求方式 请求url 请求协议/版本.

	GET/login.html HTTP/1.1

(2) obtain a demonstration of the request line

package com.hudie.web.request;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 类说明:
 * 		演示request对象获取请求行
 * @author qianliangguo
 */
@WebServlet("/requestDemo1")
public class RequestDemo1 extends HttpServlet {

	@Override
	/**
	 * request对象获取请求行的方法:
	 * 
	 * 		1.String getMethod()
	 * 			获得请求方式
	 * 		☆2.String getContextPath()
	 * 			获得虚拟路径
	 *		☆3.String getServletPath()
	 *			获得Servlet路径
	 *		4.String getQueryString()
	 *			获得get方式请求参数
	 *		☆5.String getRequestURI()
	 *			获得请求uri
	 *		☆6.String getRequestURL()
	 *			获得请求url
	 *		7.String getProtocol()
	 *			获得协议及版本
	 *		8.String getRemoteAddr()
	 *			获得客户机的IP地址
	 *
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("请求方式:"+request.getMethod());
		System.out.println("虚拟路径:"+request.getContextPath());
		System.out.println("Servlet路径:"+request.getServletPath());
		System.out.println("请求参数:"+request.getQueryString());
		System.out.println("请求URI:"+request.getRequestURI());
		System.out.println("请求URL:"+request.getRequestURL());
		System.out.println("协议及版本:"+request.getProtocol());
		System.out.println("客户机IP:"+request.getRemoteAddr());
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}
}

Here Insert Picture Description

Second, the acquisition request header

(2) a method of obtaining request header

The method of obtaining request header Explanation
String getHeader(String name) 根据请求头的名称获得请求头的值
Enumeration<String> getHeaderNames() To get the names of all request header

Request header is a key-value pairs: 请求头名称: 请求头值.

Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 12
Connection: keep-alive
Referer: http://localhost:8080/Servlet&http&request/login.html
Upgrade-Insecure-Requests: 1

(2) obtaining request header Demo

package com.hudie.web.request;


import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 类说明:
 * 		演示获得请求头数据
 * @author qianliangguo
 */

@WebServlet("/requestDemo2")
public class RequestDemo2 extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.获得所有请求头名称
		Enumeration<String> headerNames = request.getHeaderNames();
		while(headerNames.hasMoreElements()){
			//获得下一个请求头名称
			String name = headerNames.nextElement();
			//根据名称获得值
			String value = request.getHeader(name);
			System.out.println(name+":"+value);
		}
	}	
}

Here Insert Picture Description

Presentation data obtaining request header: user-agent

package com.hudie.web.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 类说明:
 * 		演示获得请求头数据:user-agent
 * @author qianliangguo
 */
@WebServlet("/requestDemo3")
public class RequestDemo3 extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String agent = request.getHeader("user-agent");
		//判断agent的浏览器版本
		if(agent.contains("Chrome")){
			System.out.println("Chorm浏览器...");
		}else if(agent.contains("Firefox")){
			System.out.println("Firefox浏览器...");
		}
	}	
}

Here Insert Picture Description

Third, the obtaining request body

package com.hudie.web.request;
import java.io.BufferedReader;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 类说明:
 * 		演示获取请求体数据
 * @author qianliangguo
 */
@WebServlet("/requestDemo4")
public class RequestDemo4 extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//获得字符流
		BufferedReader br = request.getReader();
		//读取数据
		String line = null;
		while((line = br.readLine()) != null){
			System.out.println(line);
		}
	}
}

Here Insert Picture Description

Fourth, other commonly used methods

(1) A method for a general request parameters

String getParameter(String name) 根据参数名获得参数值
String[] getParameterValues(String namee) Get an array of parameter values ​​by parameter name
Enumeration getParameterNames() Obtaining all parameters of the request name (key)
Map<String,String[]> getParameterMap() 获得所有参数的map集合

General Procedure (2) presentation request parameter obtained

<!DOCTYPE html>
<html>
  <head>
    <title>loginl.html</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  
  <body>
    <form action="requestDemo5" method="post">
    	<input name="username">
    	<input type="submit" value="提交">
    	
    	<input type="checkbox" name="hobby" value="game">游戏
    	<input type="checkbox" name="hobby" value="study">学习
    	</br>
    	<input type="submit" values="注册">
    </form>
  </body>
</html>

package com.hudie.web.request;


import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 类说明:
 * 		演示获取请求参数的通用方式
 * @author qianliangguo
 */
@WebServlet("/requestDemo5")
public class RequestDemo5 extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//根据参数名称获得值
		String username = request.getParameter("username");
		System.out.println(username);
		
		//根据参数名称获得参数值的数组
		String[] hobbies = request.getParameterValues("hobby");
		for (String hobby : hobbies) {
			System.out.println(hobby);
		}
		
		//获得所有请求的参数名称
		Enumeration<String> parameterNames = request.getParameterNames();
		while(parameterNames.hasMoreElements()){
			String name = parameterNames.nextElement();
			System.out.print(name+":");
			//然后根据参数名获得对应的value值
			String value = request.getParameter(name);
			System.out.println(value);
			System.out.println("------------end");
		}
		
		
		//获得所有参数的map集合
		Map<String, String[]> parameterMap = request.getParameterMap();
		Set<String> keyset = parameterMap.keySet();
		for (String key : keyset) {
			//根据key获取value
			String[] values = parameterMap.get(key);
			System.out.println(key);
			for (String value : values) {
				System.out.print(value+":");
			}
		}
		System.out.println("\n--------------");
		
		
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		//根据参数名称获得值
//		String username = request.getParameter("username");
//		System.out.println("GET...");
		this.doPost(request, response);
	}
	
}

Test results:
Here Insert Picture Description

Published 342 original articles · won praise 898 · Views 140,000 +

Guess you like

Origin blog.csdn.net/weixin_43691058/article/details/104023428