[Java][Web]Request 获取请求头和数据

时间:2024-09-23 13:02:56

获取方式一

InputStream in = request.getInputStream();

int len = 0;

byte buffer[] = new byte[1024];

while((len = in.read(buffer)) > 0) {

System.out.println(len);

System.out.println(new String(buffer,0,len));

}

获取方式二

String value  = request.getParameter("username");

if(value != null && !value.trim().equals("")){
    System.out.println(value);

}

获取方式三

Enumeration e = request.getParameterNames();

while(e.hasMoreElements()){

String name = (String)e.nextElement();

value = request.getParameter(name);

System.out.println(name + ":" + value);

}

获取方式四

String[] values = request.getParameterValues("username");

for(int i=0;values !=null && i < values.length;i++){

System.out.println(values[i]);

}

获取方式五

Map map = request.getParameterMap();

User user = new User();

BeanUtils.populate(user, map);

... 这里引用了 commons-beanutils-1.8.0.jar 和 commons-logging.jar 包

public class User{

public string[] getUsername(){...}

public string[] getPassword(){...}

}