MyClientServlet (注册 删除 修改 查询)
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
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 com.nanyang.entity.Customer;
import com.nanyang.service.CustomerServiceImpl;
import com.nanyang.service.CustomerServlet;
@WebServlet("/my")
public class MyClientServlet extends HttpServlet {
// 调用service层根查询客户信息
CustomerServiceImpl customerServiceImpl = new CustomerServiceImpl();
HttpServletRequest req;
HttpServletResponse reqs;
PrintWriter writer;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.req = req;
this.reqs = resp;
// 处理乱码问题
req.setCharacterEncoding("utf-8");
// 设置响应的字符集编码
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
System.out.println(req + "fsafsefges" + this.req);
String action = req.getParameter("action");
if (action.equals("zhuce")) {
zhuce();
} else if (action.equals("q")) {
q();
} else if (action.equals("d")) {
d();
} else if (action.equals("m")) {
m();
} else if (action.equals("e")) {
e();
}
}
public boolean zhuce() {
// 获取客服注册信息
String name = req.getParameter("name");
String account = req.getParameter("account");
String password = req.getParameter("password");
// 将获取的数据封装到一个客户对象在
Customer customer = new Customer(name, account, password);
boolean addCustomer = customerServiceImpl.addCustomer(customer);
if (addCustomer) {
q();
}
return false;
}
public boolean q() {
List<Customer> list = customerServiceImpl.QueryAllCustomer();
// 将客户存储在容器中
req.setAttribute("c", list);
// 转发
// 将用户使用sjp显示
try {
req.getRequestDispatcher("kehu.jsp").forward(req, reqs);
} catch (ServletException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public boolean d() {
// 获取客户的编号【删除】
String id = req.getParameter("id");
// 调用service层根据id删除客户信息
CustomerServlet customerServlet = new CustomerServiceImpl();
boolean deletion = customerServlet.DeleteCustomerByid(id);
// 判断是否删除成功
if (deletion) {
q();
}
return false;
}
public boolean m() {
// 获取客户id
String id = req.getParameter("id");
// 调用service层根据id删除客户信息
CustomerServlet customerService = new CustomerServiceImpl();
Customer customer = customerService.QueryServletByid(id);
// 将客户存储在容器中
req.setAttribute("customer", customer);
// 转发
// 将用户使用sjp显示
try {
req.getRequestDispatcher("edit.jsp").forward(req, reqs);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public boolean e() {
// 获取用户id
String id = req.getParameter("id");
String name = req.getParameter("name");
String account = req.getParameter("account");
String password = req.getParameter("password");
// 将获取的数据封装到一个客户对象在
Customer customer = new Customer(Integer.parseInt(id), name, account, password);
// 调用service层根据id改客户信息
CustomerServlet customerService = new CustomerServiceImpl();
boolean addCustomer = customerService.XiuGaiCustomerByid(customer);
if (addCustomer) {
q();
System.out.println("修改成功");
} else {
System.out.println("修改失败");
}
return false;
}
}
注册
注册成功 可以看到注册的数据
点击删除
可以看到编号31703被删除了
点击修改
把编号31708,修改成123 123 123
这里的编号不能被修改的,只能看