protected
void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
Class userClass = null;
Object user = null;
try {
userClass = Class.forName("com.example.homework2.User");
Constructor constructor = userClass.getConstructor();
user = constructor.newInstance();
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String parameterKey = parameterNames.nextElement();
String upperCase = parameterKey.toUpperCase();
String[] parameterValues = request.getParameterValues(parameterKey);
Method method = null;
String getMethod = "set" + parameterKey.replace(parameterKey.charAt(0), upperCase.charAt(0));
try {
if (parameterValues.length != 1) {
method = userClass.getMethod(getMethod, String[].class);
method.invoke(user, (Object) parameterValues);
} else {
method = userClass.getMethod(getMethod, String.class);
method.invoke(user, parameterValues[0]);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
System.out.println(user);
}