I wrote a small tool class and encapsulated the httpservletrequest request in the servlet into a pojo object. I can review the Java reflection principle. This is useless during development. After all, the MVC framework is used, the Framework comes with this function and is more powerful, but the framework should also adopt this principle. This can also be used to gain a glimpse of the framework.
This is a tool class: A pojo is automatically encapsulated by passing in the Class Object of pojo.
Package COM. xxx. xxx. util; import Java. lang. reflect. invocationtargetexception; import Java. lang. reflect. method; import Java. util. enumeration; import javax. servlet. HTTP. httpservletrequest; public class packobject <t> {class <t> C; public static <t> T GetObject (httpservletrequest request, class <t> C) {T = NULL; try {T = C. newinstance (); // instantiated parameter object} catch (instantiationexception E1) {e1.printstacktrace ();} Catch (illegalaccessexception E1) {e1.printstacktrace ();} @ suppresswarnings ("rawtypes") Enumeration E = request. getparameternames (); // all request parameters method [] Methods = C. getdeclaredmethods (); // all methods of the parameter object // convert the requested value according to the parameter type of the object's set method while (E. hasmoreelements () {string paramname = E. nextelement (). tostring (); string setparamname = reverseparamname (paramname); // convert the parameter name to the set method name, for example, convert ID to setid fo R (method: Methods) {If (setparamname. Equals (method. getname () {try {class <?> Paramtype = (method. getparametertypes () [0]; // obtain the set method parameter type string value = request. getparameter (paramname); adapter (T, method, paramtype, value); // inject values into pojo through the adapter} catch (illegalargumentexception E1) {e1.printstacktrace ();} catch (illegalaccessexception E1) {e1.printstacktrace ();} catch (invocationtargetexception E1) {e1.printstacktrace ();} catch (securityexception E1) {e1.printstacktrace () ;}}} Return t;} Private Static string reverseparamname (string paramname) {char firstchar = paramname. charat (0); char toupper = character. touppercase (firstchar); string setparamname = "set" + String. valueof (toupper) + paramname. substring (1); Return setparamname;} Private Static <t> void adapter (T, method, class <?> Paramtype, string value) throws illegalaccessexception, invocationtargetexception {If (paramtype = string. class) {method. invoke (T, value);} else if (paramtype = integer. class | paramtype = int. class) {method. invoke (T, integer. parseint (value);} else if (paramtype = long. class | paramtype = long. class) {method. invoke (T, long. parselong (value);} else if (paramtype = Boolean. class | paramtype = Boolean. class) {method. invoke (T, Boolean. parseboolean (value);} else if (paramtype = short. class | paramtype = short. class) {method. invoke (T, short. parseshort (value);} else if (paramtype = float. class | paramtype = float. class) {method. invoke (T, float. parsefloat (value);} else if (paramtype = double. class | paramtype = double. class) {method. invoke (T, double. parsedouble (value);} else if (paramtype = character. class | paramtype = char. class) {char [] cs = value. tochararray (); If (CS. length> 1) {Throw new illegalargumentexception ("the parameter length is too large");} method. invoke (T, value. tochararray () [0]) ;}}
Pojo class:
package com.xxx.xxx.util;public class User { private int id; private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", password=" + password + "]"; }}
Servlet:
package com.xxx.xxx.util;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@SuppressWarnings("serial")@WebServlet("/UserServlet")public class UserServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { User user = PackObject.getObject(request, User.class); System.out.println(user); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
Page: Enter ID: 123, name: Jay, password: Jolin
<form action="UserServlet"> id:<input name="id" /><br /> name:<input name="name" /><br /> password:<input name="password" type="password" /><br /> <input type="submit" value="submit" /> </form>
Result: User [ID = 123, name = Jay, password = Jolin]
Only int and string types are tested. No problem.