Javaweb獲取表單數(shù)據(jù)的多種方式
Javaweb獲取表單數(shù)據(jù)的幾種方式
一、通過鍵值對的形式獲取表單數(shù)據(jù)
getParameter(String name):通過key,返回一個value。
getParameterValues(String name):通過key返回一個string數(shù)組(多個值)
getParameterNames():返回form表單中的所有key值。
下面介紹通過鍵值對獲取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(); 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的形式獲取表單數(shù)據(jù)
getParameterMap():獲取form表單的數(shù)據(jù),以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("獲取表單數(shù)據(jù)之前:"+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類通過存儲器導(dǎo)出一個屬性 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的使用,點擊查看
User類
public class User { private String username;//屬性名稱需要和表單數(shù)據(jù)中的name值保持一致 private String password; private String sex; private String[] hobby; private String city; ... //set get方法省略 }
三、通過第三方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 { //通過第三方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); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SrpingDruid數(shù)據(jù)源加密數(shù)據(jù)庫密碼的示例代碼
本篇文章主要介紹了SrpingDruid數(shù)據(jù)源加密數(shù)據(jù)庫密碼的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10springboot項目docker分層構(gòu)建的配置方式
在使用dockerfile構(gòu)建springboot項目時,速度較慢,用時比較長,為了加快構(gòu)建docker鏡像的速度,采用分層構(gòu)建的方式,這篇文章主要介紹了springboot項目docker分層構(gòu)建,需要的朋友可以參考下2024-03-03如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用
這篇文章主要介紹了如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下。2021-01-01MyBatis綁定錯誤提示BindingException:Invalid bound statement (not f
這篇文章主要介紹了MyBatis綁定錯誤提示BindingException:Invalid bound statement (not found)的解決辦法,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧2017-01-01SpringBoot+Redis Bitmap實現(xiàn)活躍用戶統(tǒng)計
Redis的Bitmap數(shù)據(jù)結(jié)構(gòu)是一種緊湊的位圖,它可以用于實現(xiàn)各種場景,其中統(tǒng)計活躍用戶是一種經(jīng)典的業(yè)務(wù)場景,下面我們就來學(xué)習(xí)一下SpringBoot如何利用Redis中的Bitmap實現(xiàn)活躍用戶統(tǒng)計吧2023-11-11