🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
![](https://box.kancloud.cn/86a5c53c8cfd92a4eeaa95c4673cb1d1_536x321.png) ![](https://box.kancloud.cn/d665402fd1e94c3f6e0a3725841f4858_537x218.png) 1.添加相关jar包 ![](https://box.kancloud.cn/4b005b75429be64b1364e582d83cfeae_904x324.png) 注册页面更改 RegisterServlet.java ~~~ package zyw.web; import org.apache.commons.beanutils.BeanUtils; import zyw.bean.User; import zyw.service.UserService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Map; @WebServlet(name = "RegisterServlet",urlPatterns = "/register") public class RegisterServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String id = request.getParameter("id"); String password = request.getParameter("password"); String email = request.getParameter("email"); User user=new User(); Map<String,String[]> stringMap =request.getParameterMap(); try { //分别将属性设置到对象中 // BeanUtils.setProperty(user,name,name); //将属性的map集合封装到对象中 BeanUtils.populate(user,stringMap); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } UserService userService=new UserService(); // boolean register = userService.register(user); boolean register = userService.register(user); if (register) { response.sendRedirect(request.getContextPath()+"login.jsp"); }else { response.setContentType("text/html;charset=utf-8"); response.getWriter().write("注册失败"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } } ~~~ UserService .java ~~~ package zyw.service; import zyw.bean.User; import zyw.dao.UserDao; import java.sql.SQLException; public class UserService { //1. 判断注册用户是否存在 public boolean register(User user) { boolean register=false; UserDao userDao = new UserDao(); boolean checkUser = userDao.checkUser(user.getName()); //2. 如果不存在就将用户信息添加到数据库 if (checkUser) { register = userDao.register(user.getName(),user.getPassword(),user.getEmail()); } return register; } public User login(String name, String password) throws SQLException { UserDao userDao=new UserDao(); User user = userDao.login(name, password); return user; } } ~~~ 结果不变