当前位置:主页 > 软件编程 > JAVA代码 >

Javaweb获取表单数据的多种方式

时间:2021-01-13 09:58:32 | 栏目:JAVA代码 | 点击:

Javaweb获取表单数据的几种方式

一、通过键值对的形式获取表单数据

getParameter(String name):通过key,返回一个value。
getParameterValues(String name):通过key返回一个string数组(多个值)
getParameterNames():返回form表单中的所有key值。

下面介绍通过键值对获取form表单数据的数据的方法:

@WebServlet({ "/FormServlet", "/form" })
public class FormServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;

 public FormServlet() {
 super();
 }

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

 response.setContentType("text/json;charset=utf-8");

 PrintWriter out = response.getWriter();

 Enumeration<String> paramNames = request.getParameterNames();

 while (paramNames.hasMoreElements()) {
 String name = paramNames.nextElement();

 String[] values = request.getParameterValues(name);

 if(values!=null && values.length>0){
 StringBuilder builder = new StringBuilder(); 
 for (int i = 0; i < values.length; i++) {
  builder.append(values[i]+" ");
 }
 out.println(name+" : "+builder.toString());
 }
 }
 }

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 doGet(request, response);
 }

}

form表单:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="/TomcatDemo/form" method="post">

 用户名<input type="text" name="username"/><br/>
 密码 <input type="password" name="password"/><br/>
 性别 <input type="radio" name="sex" value="male" checked="checked"/>男
 <input type="radio" name="sex" value="female"/>女<br/>
 爱好 <input type="checkbox" name="hobby" value="basketball"/>篮球
 <input type="checkbox" name="hobby" value="football"/>足球
 <input type="checkbox" name="hobby" value="game"/>游戏
 <input type="checkbox" name="hobby" value="media"/>电影<br/>
 城市 <select name="city">
  <option value="bj">北京</option>
  <option value="sh">上海</option>
  <option value="sz">深圳</option>
  <option value="hz">杭州</option>
  </select><br/>
  <input type="submit" value="注册"/>
  <input type="submit" value="登入"/><br/>

 </form>
</body>
</html>

二、通过Map的形式获取表单数据

getParameterMap():获取form表单的数据,以map的格式封装起来

示例:

@WebServlet({ "/FormServlet", "/form" })
public class FormServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;

 public FormServlet() {
 super();
 }

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

 response.setContentType("text/json;charset=utf-8");

 PrintWriter out = response.getWriter();

 User user = new User();
 out.println("获取表单数据之前:"+user.toString());

 Map<String,String[]> map = request.getParameterMap();

 for (Map.Entry<String, String[]> m : map.entrySet()) {

 String name = m.getKey();
 String[] values = m.getValue();
 //属性描述器:表示JavaBean类通过存储器导出一个属性
 PropertyDescriptor pd=null;
 try {
  pd = new PropertyDescriptor(name, User.class);
 } catch (IntrospectionException e) {
 e.printStackTrace();
 }

 if (values!=null&& pd !=null) {

 Method setter = pd.getWriteMethod();
 try {
  if (values.length==1) {
  setter.invoke(user, values[0]);
  }else {
  setter.invoke(user, (Object)values);
  }
 } catch (IllegalAccessException e) {
  e.printStackTrace();
 } catch (IllegalArgumentException e) {
  e.printStackTrace();
 } catch (InvocationTargetException e) {
  e.printStackTrace();
 }
 }
 }
 out.println("获取表单数据之后:"+user.toString());
 }

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 doGet(request, response);
 }
}

PropertyDescriptor的使用,点击查看

User类

public class User {
 private String username;//属性名称需要和表单数据中的name值保持一致
 private String password;
 private String sex;
 private String[] hobby;
 private String city;
 ...
 //set get方法省略
}

三、通过第三方jar包获取封装表单数据

使用第三方jar包:commons-beanutils-1.8.3.jar

@WebServlet({ "/FormServlet", "/form" })
public class FormServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;

 public FormServlet() {
 super();
 }

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

 response.setContentType("text/json;charset=utf-8");

 PrintWriter out = response.getWriter();

 User user = new User();
 out.println("获取表单数据之前:"+user.toString());

 try {
 //通过第三方jar包处理
 BeanUtils.populate(user, request.getParameterMap());
 } catch (IllegalAccessException e) {
 e.printStackTrace();
 } catch (InvocationTargetException e) {
 e.printStackTrace();
 }

 out.println("获取表单数据之后:"+user.toString());

 }


 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 doGet(request, response);
 }

}

您可能感兴趣的文章:

相关文章