------------------------------------------------RequestDemo2.java
<pre name="code" class="java">package cn.ITHong.Request;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import cn.ITHong.domain.User;
//获取请求参数
public class RequestDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
/**
* 以流的形式获取请求正文,<span style="color:#ff0000;">最基础的。。。。。。。</span>*/
private void test7(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletInputStream in = request.getInputStream();
int len = -1 ;
byte[] b = new byte[1024];
while((len = in.read(b))!=-1)
System.out.println(new String(b,0,len));
in.close();
}
/**
* 获取所有参数和取值:实际开发中,<span style="color:#ff0000;">把数据封装到JavaBean中,非常简单,借助BeanUtils框架</span>。
* 直接把logging和
* Beanutil导入WebRoot->WEB-INF->lib就可以,他会自动生成到Web App Libraries
*/
private void test6(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User user = new User();
System.out.println("封装前:"+user);
try {
/**
* 实现原理就是test5(),编写代码时,请保持
* 表单输出与JavaBean的属性一致即可。*/
BeanUtils.populate(user, request.getParameterMap());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("封装后:"+user);
}
/**
* 获取所有参数和取值:实际开发中,把数据封装到JavaBean中,SUN推荐做法,但是<span style="color:#ff0000;">不精简</span>
*/
private void test5(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User user = new User();
System.out.println("封装前:"+user);
Map<String, String[]> map = request.getParameterMap();
for(Map.Entry<String, String[]> me:map.entrySet())
{
String paramName = me.getKey();
String[] values = me.getValue();
try {
/**直接传进去paramName很好,因为我压根就不知道是先传username
* 还是password,程序自己判断.同时方便在User类中添加新的"字段",
* 也就是添加新的"字段"随便加,压根就不用修改下列的编程
*/
PropertyDescriptor pd = new PropertyDescriptor(paramName,
User.class);
//依照前面传进来的paramName自己寻找setter方法
Method m = pd.getWriteMethod();
if(!(values.length>1))
m.invoke(user, values);
else {
m.invoke(user, (Object)values);
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
System.out.println("封装后:"+user);
}
/**
* 获取所有参数和取值:实际开发中,把数据封装到JavaBean中
*/
private void test4(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Enumeration<String> e = request.getParameterNames();// 所有的请求参数名
User user = new User();
System.out.println("封装前。。。"+user);
//表单输入域的名称与JavaBean中的属性名称一致:使用内省,方便扩张。
while (e.hasMoreElements()) {
String paramName = (String) e.nextElement();
String[] values = request.getParameterValues(paramName);
try {
/**直接传进去paramName很好,因为我压根就不知道是先传username
* 还是password,程序自己判断.同时方便在User类中添加新的"字段",
* 也就是添加新的"字段"随便加,压根就不用修改下列的编程
*/
PropertyDescriptor pd = new PropertyDescriptor(paramName,
User.class);
//依照前面传进来的paramName自己寻找setter方法
Method m = pd.getWriteMethod();
if(!(values.length>1))
m.invoke(user, values);
else {
m.invoke(user, (Object)values);
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
System.out.println("封装后。。。"+user);
}
/**
* 获取所有参数和取值:实际开发中,把数据封装到JavaBean中
*/
private void test3(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Enumeration<String> e = request.getParameterNames();// 所有的请求参数名
while (e.hasMoreElements()) {
String paramName = (String) e.nextElement();
String[] values = request.getParameterValues(paramName);
System.out.println(paramName + "..." + Arrays.asList(values));
}
}
/**
* 获取指定重名参数的值
*/
private void test2(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String[] usernames = request.getParameterValues("username");
String[] passwords = request.getParameterValues("password");
System.out.println("用户名:");
for (String es : usernames)
System.out.println(es);
System.out.println("密码:");
for (String es : passwords)
System.out.println(es);
}
/**
* 获取指定单一参数的值
*/
private void test1(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* html可以重复定义同名字不同内容的变量,但是以下方法却只能接受第一个传过来的值
*/
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("用户名:" + username + "密码:" + password);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test7(request, response);
}
}
-----------------------------------------------------------------------------------------------------User.java(JavaBean方法)
package cn.ITHong.domain;
import java.util.Arrays;
public class User {
private String username;
private String[] password;
private String gender;
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String[] getPassword() {
return password;
}
public void setPassword(String[] password) {
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password="
+ Arrays.toString(password) + ", gender=" + gender + "]";
}
}
-------------------------------------------------------------------------------------------------------------regist.html
<!DOCTYPE html>
<html>
<head>
<title>regist.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="/Web07/servlet/RequestDemo2" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
请重新输入密码:<input type="password" name="password"/><br/>
请选择性别:<input type="text" name="gender"/><br/>
<input type="submit" value="注册"/>
</form>
</body>
</html>