Package CN. jbit. util; import Java. beans. introspectionexception; import Java. beans. propertydescriptor; import Java. io. unsupportedencodingexception; import Java. lang. reflect. field; import Java. lang. reflect. method; import java.net. urldecoder; import Java. text. simpledateformat; import Java. util. collection; import Java. util. date; import Java. util. enumeration; import Java. util. hashmap; import Java. util. map; import javax. imageIO. SPI. serviceregistry. filter; import javax. servlet. HTTP. httpservletrequest; public class requestutil {public static object GetObject (httpservletrequest request, class clazz, string ENC) throws unsupportedencodingexception, introspectionexception, instantiationexception, detail {field [] fields = clazz. getdeclaredfields (); object OBJ = clazz. newinstance (); Request. setcharacterencoding (ENC); Map <string, string> map = new hashmap <string, string> (); If (request. getmethod (). equals ("get") {system. out. println ("----------- get --------------------"); string query = urldecoder. decode (request. getquerystring (), ENC); string [] STR = query. split ("&"); For (string: Str) {try {map. put (string. split ("=") [0], String. split ("=") [1]);} catch (exception e) {e. printstacktrace () ;}} else {system. out. println ("----------- post --------------------"); Map <string, string []> parmmap = request. getparametermap (); For (string key: parmmap. keyset () {try {map. put (Key, parmmap. get (key) [0]);} catch (exception e) {e. printstacktrace () ;}}for (field: fields) {try {propertydescriptor Pd = new propertydescriptor (field. getname (), clazz); Method seter = PD. getwritemethod (); If (field. getType (). getname (). equals (Int. class. getname () | field. getType (). getname (). equals (integer. class. getname () {seter. invoke (OBJ, integer. valueof (map. get (field. getname ();} else if (field. getType (). getname (). equals (date. class. getname () {simpledateformat SDF = new simpledateformat (); Date = SDF. parse (map. get (field. getname (); seter. invoke (OBJ, date);} else if (field. getType (). getname (). equals (double. class. getname () | field. getType (). getname (). equals (double. class. getname () {seter. invoke (OBJ, double. valueof (map. get (field. getname ();} else if (field. getType (). getname (). equals (string. class. getname () {seter. invoke (OBJ, map. get (field. getname () ;}} catch (exception e) {system. out. println ("abnormal") ;}} return OBJ ;}}
Automatically encapsulate form fields through reflection