JAVAWeb

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

 这里的编号不能被修改的,只能看

 

 修改成功后

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值