Javaweb獲取表單數(shù)據(jù)的幾種方式
創(chuàng)新互聯(lián)長(zhǎng)期為千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為文縣企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、網(wǎng)站制作,文縣網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
一、通過(guò)鍵值對(duì)的形式獲取表單數(shù)據(jù)
getParameter(String name):通過(guò)key,返回一個(gè)value。
getParameterValues(String name):通過(guò)key返回一個(gè)string數(shù)組(多個(gè)值)
getParameterNames():返回form表單中的所有key值。
下面介紹通過(guò)鍵值對(duì)獲取form表單數(shù)據(jù)的數(shù)據(jù)的方法:
@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
二、通過(guò)Map的形式獲取表單數(shù)據(jù)
getParameterMap():獲取form表單的數(shù)據(jù),以map的格式封裝起來(lái)
示例:
@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("獲取表單數(shù)據(jù)之前:"+user.toString()); Mapmap = request.getParameterMap(); for (Map.Entry m : map.entrySet()) { String name = m.getKey(); String[] values = m.getValue(); //屬性描述器:表示JavaBean類通過(guò)存儲(chǔ)器導(dǎo)出一個(gè)屬性 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("獲取表單數(shù)據(jù)之后:"+user.toString()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
PropertyDescriptor的使用,點(diǎn)擊查看
User類
public class User { private String username;//屬性名稱需要和表單數(shù)據(jù)中的name值保持一致 private String password; private String sex; private String[] hobby; private String city; ... //set get方法省略 }
三、通過(guò)第三方j(luò)ar包獲取封裝表單數(shù)據(jù)
使用第三方j(luò)ar包: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("獲取表單數(shù)據(jù)之前:"+user.toString()); try { //通過(guò)第三方j(luò)ar包處理 BeanUtils.populate(user, request.getParameterMap()); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } out.println("獲取表單數(shù)據(jù)之后:"+user.toString()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。