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

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

创新互联长期为千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为文县企业提供专业的网站建设、网站制作文县网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。

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

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 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表单:





Insert title here


 
用户名
密码
性别
爱好 篮球 足球 游戏 电影
城市

二、通过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 map = request.getParameterMap();

 for (Map.Entry 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);
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


文章名称:Javaweb获取表单数据的多种方式
文章路径:http://csdahua.cn/article/psdjho.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流