YuFeng

BeanUtils
工具包:BeanUtils.zippackage cn; import java.io.IOException;...
扫描右侧二维码阅读全文
20
2019/06

BeanUtils

工具包:BeanUtils.zip

package cn;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;
import java.util.UUID;

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 org.apache.commons.dbutils.QueryRunner;

public class RegisterServlet extends HttpServlet {

    private static final long serialVersionUID = -3872820192646889994L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println(user.toString());
        user.setId(UUID.randomUUID().toString());
        String regiester = regiester(user);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(regiester);
    }
    
    @SuppressWarnings("finally")
    private String regiester(User user) {
        QueryRunner qr = new QueryRunner(MyDataSource.getDataSource());
        String sql = "insert into user values(?,?,?,?,?)";
        int update = 0;
        try {
            System.out.println(user.getId());
            update = qr.update(sql,user.getId(),user.getName(),user.getEmail(),user.getUsername(),user.getPassword());
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(update > 0) {
                return "注册成功";
            }else {
                return "注册失败";
            }
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
Last modification:June 20th, 2019 at 10:59 am
If you think my article is useful to you, please feel free to appreciate

Comment here is closed