一、前端HTML
1.1、收集表单数据
当点击添加按钮时,使用axios将表单数据发送到servlet中
1.2、处理axios响应的结果
根据respones.data的结果进行判断
1.3、完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加用户</title>
<!-- 导入bootstrap依赖 -->
<link rel="stylesheet" href="assets/bootstrap-3.3.7-dist/css/bootstrap.min.css">
<script src="assets/jquery-3.5.1/jquery-3.5.1.min.js"></script>
<script src="assets/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<!-- 导入vue依赖 -->
<script src="assets/axios.min.js"></script>
<script src="assets/vue.min-v2.5.16.js"></script>
<style>
a:link{font-size: 20px;color: rgb(109,109,109);}
a:visited{font-size: 20px;color: rgb(109,109,109);}
a:hover{font-size: 20px;color: white;text-decoration: none}
</style>
</head>
<body>
<div class="container" id = "app">
<!-- bootstrap行 -->
<div class="row">
<!-- 显示导航 -->
<div class="col-md-3" style="background-color: rgb(0,21,41);height: 800px">
<div class="row">
<div class="col-md-12" style="background-color: rgb(0,40,77);text-align: center;height: 70px;line-height: 70px;font-size: 20px;color: white;font-weight: bold">
<img src="assets/img/logo.png" style="width: 30px;height: 30px;margin-right: 8px"/>
蜗牛CRM管理系统
</div>
</div>
<div class="row">
<div class="col-md-12" style="text-align: center;padding: 20px 0px 20px 0px;">
<a href="index.html">进入首页</a>
</div>
</div>
<div class="row">
<div class="col-md-12" style="text-align: center;padding: 20px 0px 20px 0px;">
<a href="user_list.html">用户管理</a>
</div>
</div>
<div class="row">
<div class="col-md-12" style="text-align: center;padding: 20px 0px 20px 0px;">
<a href="user_add.html">添加用户</a>
</div>
</div>
<div class="row">
<div class="col-md-12" style="text-align: center;padding: 20px 0px 20px 0px;">
<a href="#">客户管理</a>
</div>
</div>
<div class="row">
<div class="col-md-12" style="text-align: center;padding: 20px 0px 20px 0px;">
<a href="#">添加客户</a>
</div>
</div>
<div class="row">
<div class="col-md-12" style="text-align: center;padding: 20px 0px 20px 0px;">
<a href="user_alert.html">修改密码</a>
</div>
</div>
<div class="row">
<div class="col-md-12" style="text-align: center;padding: 20px 0px 20px 0px;">
<a href="login.html">退出登录</a>
</div>
</div>
</div>
<!-- 显示内容 -->
<div class="col-md-9" style="border: 1px solid gray;height: 800px">
<div class="row">
<!-- 显示提示位置 -->
<div class="col-md-12" style="height: 70px ;font-size: 18px;color: rgb(109,109,109);line-height: 70px;font-weight: bold;padding-left: 20px">
> 添加用户
</div>
</div>
<div class="row" style="background-color: rgb(240,242,245);height: 730px;padding:20px">
<!-- 显示内容 -->
<div class="col-md-12" style="background-color: white;height: 730px;border: none;border-radius: 5px">
<div class="row">
<div class="col-md-4 col-md-offset-4" style="margin-top: 50px">
<label>用户名</label>
<input type="text" class="form-control" v-model="userName"/>
</div>
</div>
<div class="row">
<div class="col-md-4 col-md-offset-4" style="margin-top: 20px">
<label>昵称</label>
<input type="text" class="form-control" v-model="nickName"/>
</div>
</div>
<div class="row">
<div class="col-md-4 col-md-offset-4" style="margin-top: 20px">
<label>手机号</label>
<input type="text" class="form-control" v-model="phone"/>
</div>
</div>
<div class="row">
<div class="col-md-4 col-md-offset-4" style="margin-top: 20px">
<label>生日</label>
<input type="text" class="form-control" v-model="birth"/>
</div>
</div>
<div class="row">
<div class="col-md-4 col-md-offset-4" style="margin-top: 20px">
<label style="margin-right: 15px">性别</label>
<label class="radio-inline">
<input type="radio" name="sex" value="0" v-model="sex"/>男
</label>
<label class="radio-inline">
<input type="radio" name="sex" value="1" checked="true" v-model="sex"/>女
</label>
</div>
</div>
<div class="row">
<div class="col-md-4 col-md-offset-4" style="margin-top: 20px ;text-align: center;">
<button class="btn btn-primary" style="margin-right: 8px" @click="doAdd">添加</button>
<button class="btn btn-default" @click="doReset">重置</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
new Vue({
el:'#app',
data:{
userName:null,
nickName:null,
phone:null,
birth:null,
sex:null
},
methods:{
doAdd(){
// 通过axios发送请求到UserLoginServlet
var url = "http://localhost:8080/add?user_name=" + this.userName + "&nick_name=" + this.nickName + "&phone=" + this.phone + "&birth=" + this.birth + "&sex=" + this.sex;
axios.get(url).then(response => {
if (response.data == '添加成功') {//添加成功
window.location.href = 'user_list.html';//跳转到主页user_list.html
} else {//添加失败
alert("输入有误!!!");//弹窗警告
}
})
},
doReset(){
this.userName = null;
this.nickName = null;
this.phone = null;
this.birth = null;
this.sex = null
}
},
created:function(){
}
})
</script>
</body>
</html>
二、后端Servlet
2.1、在UserDAO中新增添加用户的方法![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/327ce55dc9b3f94be94fb726ba98950d.png)
2.2、创建UserAddServlet实现添加用户的功能
package controller;
import dao.Impl.UserDaoImpl;
import dao.UserDao;
import entity.User;
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.io.PrintWriter;
@WebServlet("/add")
public class UserAddServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("application/json;charset=utf8");
PrintWriter writer = resp.getWriter();
UserDao userDao = new UserDaoImpl();
String userName = req.getParameter("user_name");
String nickName = req.getParameter("nick_name");
String phone = req.getParameter("phone");
String birth = req.getParameter("birth");
int sex = Integer.parseInt(req.getParameter("sex"));
User user = new User(0,userName,"666666",nickName,sex,phone,"www.baidu.com",birth);
int add = userDao.add(user);
if(add > 0){
writer.print("添加成功");
}else {
writer.print("添加失败");
}
writer.close();
}
}