符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
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(); EnumerationparamNames = 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()); Mapmap = 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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。