/********************************************************servlet页面****************************************************************/
package request;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import bean.User;
import com.sun.org.apache.regexp.internal.REUtil;
//获取请求头
//获取请求数据的时候一般都要先检查,再使用
public class RequestDemo2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取客户端超链接或表单提交过来的数据
//1,直接获取
String value = request.getParameter("username");
//先判断value的值是否符合要求,再使用
if(value!=null && !value.trim().equals(""))
System.out.println(value);
System.out.println("--------------");
//通过提交数据的名称获得值
//2,先得到所有名称,再根据名称获得值
Enumeration e = request.getParameterNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
value = request.getParameter(name);
System.out.println(name +" = "+value);
}
System.out.println("--------------");
//3,获取指定名称的所有值
String[] values = request.getParameterValues("username");
//这样写程序健壮性不够,如果客户机表单没有填写数据就提交,此时values就为空
//执行下面的语句就会报空指针异常
// for(String v:values){
// System.out.println(v);
// }
//这样写,才不会报措
for(int i=0;values!=null && i<values.length;i++){
System.out.println(values[i]);
}
//4
Map map = request.getParameterMap();
User user = new User();
try {
BeanUtils.populate(user, map);
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println(user);
}
//获取头相关的数据
private void test1(HttpServletRequest request) {
//获取头信息
//1
String headValue = request.getHeader("Accept-Encoding");
System.out.println(headValue);
System.out.println("-----------------------");
//2
Enumeration e = request.getHeaders("Accept-Encoding");
while(e.hasMoreElements()){
String value = (String) e.nextElement();
System.out.println(value);
}
System.out.println("-----------------");
//3
e = request.getHeaderNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
String value = request.getHeader("name");
System.out.println(value);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
/********************************************************html页面**********************************************************************************/
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>提交数据</title>
</head>
<body>
<!-- 超链接给服务端带数据 -->
<a href="/requestandresponse/RequestDemo2?username=xxxxxx">点点</a>
<!-- 表单给服务器带数据 -->
<form action="/requestandresponse/RequestDemo2" method="post">
用户名:<input type="text" name="username"/>
<input type="submit" value="提交">
</form>
</body>
</html>