Request获取参数封装方式
生活随笔
收集整理的這篇文章主要介紹了
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 平面设计电脑主板(平面设计电脑主机推荐)
- 下一篇: 响应对象Response