controller层
public class RegisterServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/**
* 1.获取参数
* 2.
* 可用:
* 调用service保存到数据库
*
* 不可用:
* 跳回注册页面
*/
//1.获取参数
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html; charset=UTF-8");
String username = req.getParameter("username");
System.out.println(username);
String phone = req.getParameter("phone");
System.out.println(phone);
String password = req.getParameter("password");
System.out.println(password);
// resp.getWriter().write("注册成功了");
UserDaoImpl userDaoImpl= new UserDaoImpl();
UserService userService=new UserServiceImpl(userDaoImpl);
userService.add(new User(username,phone,password));
//跳回注册页面
// req.getRequestDispatcher("/html/register.html").forward(req,resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
service层
userService接口
public interface UserService {
//添加用户
int add(User user);
}
UserServiceImpl实现类
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
public UserServiceImpl() {
}
@Override
public int add(User user) {
int add=userDao.insert(user);
return add;
// userDao.insert(user);
}
}
dao层
userDao接口
public interface UserDao {
int insert(User user);
}
userDaoImpl实现类
public class UserDaoImpl implements UserDao {
@Override
public int insert(User user) {
String username=user.getUsername();
String phone=user.getPhone();
String password = user.getPassword();
String sql="INSERT INTO tb_user(username,phone,password)VALUES(?,?,?)";
int i= DbUtil.execteUpdate(sql,username,phone,password);
return i;
}
}
entity实体类
public class User {
private String username;
private String phone;
private String password;
public User(){}
public User(String username, String phone, String password) {
this.username = username;
this.phone = phone;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
util工具类
public class DbUtil {
private static Properties properties;
static{
properties=new Properties();
try {
properties.load(new FileInputStream("resources/db.properties"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConn(){
Connection connection=null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//读取db.properties配置文件,得到一个Input流
InputStream resourceAsStream = DbUtil.class.getClassLoader().getResourceAsStream("db.properties");
//从流中加载数据
properties.load(resourceAsStream);
connection = DriverManager.getConnection(properties.getProperty("url"),
properties.getProperty("user"), properties.getProperty("password"));
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static int execteUpdate(String sql,Object...paras){
Connection conn=getConn();
int i=0;
try {
PreparedStatement preparedStatement = conn.prepareStatement(sql);
if (paras!=null && paras.length>0){
for (int j=0;j<paras.length;j++){
preparedStatement.setObject(j+1,paras[j]);
}
}
i=preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return i;
}
web界面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<form action="/aaa/reg" method="post">
姓名:<input type="text" name="username" placeholder="username"><br>
电话:<input type="text" name="phone" placeholder="phone"><br>
密码:<input type="password" name="password" placeholder="password"><br>
<input type="submit" value="注册">
</form>
</div>
</body>
</html>