获取表单值COPY到实体Bean

package com.cstp.hy.user.test;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.beanutils.BeanUtils;
 
import com.cstp.hy.user.model.bo.Users;
import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;
 
public class TestServlet extends HttpServlet {
 
    private static final long serialVersionUID = -4940132974326780996L;
 
    private static final Log log = LogFactory.getLog

(TestServlet.class);
 
    public TestServlet() {
       super();
    }
 
    public void destroy() {
       super.destroy();
    }
 
    public void doPost(HttpServletRequest request, HttpServletResponse

response)
           throws ServletException, IOException {
 
       try {
           request.setCharacterEncoding("utf-8");
 
           String m_key = "";
           String m_value = "";
 
           Users user = new Users();
 
           Map<String, String> map = new HashMap<String, String>();
           Enumeration e_names = request.getParameterNames();
 
           while (e_names.hasMoreElements()) {
              m_key = (String) e_names.nextElement();
              String[] e_value = request.getParameterValues(m_key);
              for (int i = 0; i < e_value.length; i++) {
                  m_value = e_value[i];
              }
              map.put(m_key, m_value);
           }
           BeanUtils.populate(user, map);
 
           log.debug("id=" + user.getId());
           log.debug("username=" + user.getUserName());
           log.debug("password=" + user.getPassWord());
 
       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
       } catch (IllegalAccessException e) {
           e.printStackTrace();
       } catch (InvocationTargetException e) {
           e.printStackTrace();
       }
    }
 
    public void init() throws ServletException {
    }
 
}
 

你可能感兴趣的:(职场,休闲)