一、后端servlet开发
1.1、创建Custom实体类
public class Custom {
private int id;
private String name;
private int age;
private int Sex;
private String phone;
private String wechat;
private String addr;
private String hoby;
private String email;
private String occupation;
private int uid;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSex() {
return Sex;
}
public void setSex(int sex) {
Sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getWechat() {
return wechat;
}
public void setWechat(String wechat) {
this.wechat = wechat;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getHoby() {
return hoby;
}
public void setHoby(String hoby) {
this.hoby = hoby;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
}
1.2、创建CustomDAO,新增用户的方法
public class CustomDAOImpl extends BaseDAO implements CustomDAO {
public int insertCustom(Custom custom) {
String sql = "insert into t_custom (name,age,sex,phone,wechat,addr,hoby,email,occupation,uid) values ('"+custom.getName()+"',"+custom.getAge()+","+custom.getSex()+",'"+custom.getPhone()+"','"+custom.getWechat()+"','"+custom.getAddr()+"','"+custom.getHoby()+"','"+custom.getEmail()+"', '"+custom.getOccupation()+"',"+custom.getUid()+")"; int count = this.executeUpdate(sql);
return count;
}
}
1.3、创建CustomAddServlet
第1步:在session中获取用户id
//从session中获取用户的id
HttpSession session = req.getSession();
Manger manger =(Manger) session.getAttribute("user");
int uid=manger.getId();
第2步:将用户信息通过dao保存到数据库
CustomDAO dao=new CustomDAOImpl();
//获取请求传参
String name = req.getParameter("name");
int age = Integer.parseInt(req.getParameter("age"));
int sex = Integer.parseInt(req.getParameter("sex"));
String phone = req.getParameter("phone");
String wechat = req.getParameter("wechat");
String addr = req.getParameter("addr");
String hoby = req.getParameter("hoby");
String email = req.getParameter("email");
String occupation = req.getParameter("occupation");
Custom custom = new Custom();
custom.setName(name);
custom.setAge(age);
custom.setSex(sex);
custom.setPhone(phone);
custom.setWechat(wechat);
custom.setAddr(addr);
custom.setHoby(hoby);
custom.setEmail(email);
custom.setOccupation(occupation);
custom.setUid(uid);
int count = dao.insertCustom(custom);
if(count>0){
writer.print("保存成功");
}else{
writer.print("保存失败");
}
writer.close();//释放资源
}
二、前端HTML开发
2.1、通过vue的双向绑定收集用户信息
data:{
customName: null,
age: null,
sex: null,
phone: null,
wechat: null,
addr: null,
hoby: null,
email: null,
occupation: null
},
2.2、给添加按钮绑定点击事件
<button class="btn btn-primary" style="margin-right: 8px;" @click="doAdd()">添加</button>
2.3、通过axios发送请求访问servlet添加客户
doAdd(){
var url = "http://localhost:8080/custom_add?name="+this.customName+"&age="+this.age+"&sex="+this.sex+"&phone="+this.phone+"&wechat="+this.wechat+"&addr="+this.addr+"&hoby="+this.hoby+"&email="+this.email+"&occupation="+this.occupation;
console.log(url);
axios.get(url).then(response=>{
if(response.data=="保存成功"){
alert("自己查数据库");
//window.location.href="user_list.html"
}else{
alert('添加失败!!!');
}
})
},