In the Java EE Web, we collect data from the form table only son, usually in the form of a value object, to get, encapsulate, and then upload the value object to the background for use.
Such as:
<span style= "FONT-SIZE:18PX;" > String name=request.getparameter ("name"); String pwd=request.getparameter ("pwd"); String ID =request.getparameter ("id"); User User =new User (); User.setuid (ID); user.setname (name); User.setpwd (PWD);</span>
It's been a lot of trouble every time, and now we have new technology .
This can be achieved with both packages.
<span style= "FONT-SIZE:18PX;" >public void Login (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {map< String, string[]> map=req.getparametermap (); User user = Beanutils.populate (user.class, map); System.out.println (user);</span>
In the servlet we just need to call to do, in the user value object, our foreground parameters can be set, there is no need to ignore.
But we use this need to note: front page code <input type= "text" name= "name"/> This parameter must be aligned, and the value object to be consistent, otherwise it will not succeed, Because in class reflection, we all get the name of the variable, and then we call the set+ variable name one capitalization to set.
<span style= "FONT-SIZE:18PX;" >public class Beanutils {public static <t>t populate (T t,map<string,string[]> Map) {try{ Org.apache.commons.beanutils.BeanUtils.populate (T,MAP); return t;} catch (Exception e) {throw new RuntimeException (E.getmessage (), E);}} public static <t>t populate (class<t> cls,map<string,string[]> Map) {try{t T = cls.newinstance (); Return populate (t, map);} catch (Exception e) {throw new RuntimeException (E.getmessage (), E);}} </span>
abovewith two packets, just pass the corresponding parameter, and we can package it accordingly (in fact, it is the class reflection) .
Two packages.
Java EE------------class reflection directly encapsulates the parameters passed by the foreground.