日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Request获取参数封装方式

發(fā)布時間:2023/12/3 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Request获取参数封装方式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

瀏覽器請求界面

1.獲取參數(shù)手動封裝數(shù)據(jù)
@WebServlet("/ServletDemo4") public class ServletDemo4 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//根據(jù)參數(shù)名獲取參數(shù)值String name = req.getParameter("name");String password = req.getParameter("password");String[] hobby = req.getParameterValues("hobby");//將數(shù)據(jù)封裝到學(xué)生對象中Student stu = new Student(name, password, hobby);System.out.println(stu);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }打印結(jié)果: ---------------------------------------------------------- Student{name='zhangsan', age='123', hobby=[study, game, book]}
2.通過反射封裝數(shù)據(jù)
//通過反射封裝數(shù)據(jù) @WebServlet("/ServletDemo5") public class ServletDemo5 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.獲取所有參數(shù)的信息Map<String, String[]> parameterMap = req.getParameterMap();Set<String> keys = parameterMap.keySet();//2.將數(shù)據(jù)封裝到學(xué)生對象中Student stu = new Student();//遍歷集合for (String key : keys) {String[] value = parameterMap.get(key);//獲取學(xué)生對象的屬性描述器[可以獲取對應(yīng)變量的相應(yīng)方法]try {PropertyDescriptor pd = new PropertyDescriptor(key, stu.getClass());//獲取setXxx方法;把屬性名首字母變大寫,加上一個set前綴,作為方法名Method method = pd.getWriteMethod();//執(zhí)行方法給屬性賦值;需要判斷屬性對應(yīng)的值是否>1[]if(value.length>1){//參數(shù)個數(shù)不匹配,因此會報異常wrong number of arguements//正確的調(diào)用方法是先將String數(shù)組強(qiáng)制轉(zhuǎn)換成Object,然后傳參數(shù)method.invoke(stu,(Object)value);}else {method.invoke(stu,value);}//System.out.println(stu); 每次屬性賦值都打印} catch (Exception e) {e.printStackTrace();}System.out.println(stu);}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }打印結(jié)果: ---------------------------------------------------------- Student{name='zhangsan', age='123', hobby=[study, game, book]}
3.通過工具類(包)封裝數(shù)據(jù)【常用】
//通過工具類(包)封裝數(shù)據(jù)- @WebServlet("/ServletDemo6") public class ServletDemo6 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.獲取所有參數(shù)的信息Map<String, String[]> parameterMap = req.getParameterMap();//2.將數(shù)據(jù)封裝到學(xué)生對象中Student stu = new Student();//利用BeanUtils工具類,給學(xué)生對象的屬性賦值try {BeanUtils.populate(stu,parameterMap);System.out.println(stu);} catch (Exception e) {e.printStackTrace();}System.out.println(stu);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }Student{name='zhangsan', age='123', hobby=[study, game, book]}

BeanUtils工具類資源

總結(jié)

以上是生活随笔為你收集整理的Request获取参数封装方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。