request:获取请求头和请求参数
获取请求头:
//获取头相关信息
private void test1(HttpServletRequest request) {
//拿到指定符合的头信息
String header=request.getHeader("no-cache");
System.out.println("header-->"+header);
System.out.println("----------------------------");
//如果相同的头存在多个,依次取出来
Enumeration en=request.getHeaders("Accept-Encoding");
while(en.hasMoreElements()){
String value=(String) en.nextElement();
System.out.println("value-->"+value);
}
System.out.println("----------------------------");
//拿到所有头信息
Enumeration e=request.getHeaderNames();
while(e.hasMoreElements()){
String name=request.getHeader((String) e.nextElement());
System.out.println("name-->"+name);
System.out.println("----------------------------");
}
}
------------------------------------------------------------------------------
客户机带数据给服务器:
1、通过超链接:
<a href="/servletdemo/servlet/Request1?username=xx ">点点</a>
2、通过表单的方式:
<form action ="/servletdemo/servlet/Request1" method="post">
用户名:<input type="text" name="username">
密码:<input type="text" name="password">
<input type="submit" value="提交">
</form>
---------------------------------------------------------------------------------------
获取请求数据:
String value = request.getParameter("username");
if (value != null && !value.trim().equals("")) {
System.out.println(value);
}
System.out.println("----------------方式一----------------------");
Enumeration en = request.getParameterNames();
while (en.hasMoreElements()) {
String name = (String) en.nextElement();
String va = request.getParameter(name);
System.out.println(name + "--->" + va);
}
System.out.println("---------------方式二-----------------------");
// 获取指定名称的所有数据
String[] ss = request.getParameterValues("username");
for (int i = 0; ss != null && i < ss.length; i++) {
System.out.println(ss[i]);
}
System.out.println("---------------方式三-----------------------");
System.out.println("---------------方式三-----------------------");
}