package com.yh.controller;
import com.yh.model.Customer;
import com.yh.service.CustomerService;
import com.yh.service.impl.CustomerServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
@WebServlet("/AddCustomerServlet")
public class AddCustomerServlet extends ViewBaseServlet{
CustomerService customerService=new CustomerServiceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 登录认证思路 登陆成功之后 在session上绑定属性
// 以后操作时先获取对应的属性:获取到:表示通过
// 获取不到:表示未登录 跳转到登录页面
// Object user = request.getSession().getAttribute("user");
// if (user != null) {
// // 尚未登陆
// response.sendRedirect("/login&®ist.html");
// return;
// }
Customer customer=new Customer();
//获取请求参数
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//获取请求参数
Map<String,String[]> map=request.getParameterMap();
try {
//把参数封装成对象
BeanUtils.populate(customer, map);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
int i = customerService.register(customer);
if (i==1){
request.setAttribute("msg","邮箱已被占用!!!");
request.setAttribute("customer",customer);
// request.getRequestDispatcher("add,html").forward(request,response);
super.processTemplate("add",request,response);
}else {
//重定向
response.sendRedirect(request.getContextPath()+"/QueryPageCustomerServlet");
}
}
}
标签:customer,yh,java,request,用户,添加,new,import,response
From: https://blog.51cto.com/u_14978586/7491399