添加操作
复杂验证:验证需要通过业务层来完成,一般需要通过数据库服务器。
JSP代码:
用户名占用提示:
<% Object msg= request.getAttribute("message");
if(msg!=null){
%>
<br>
<font color="red"><%= msg %></font>
<br>
<br>
<%
}
%>
回显:
<td><input type="text" name="name" value="<%= request.getParameter("name") == null ? "" :request.getParameter("name")%>" /></td>
Servlet代码:
private void addCustomer(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//1.获取表单参数:name,address,phone
String name=request.getParameter("name");
String address=request.getParameter("address");
String phone=request.getParameter("phone");
//2.检验name 是否已经被占用:
//2.1调用CustomerDAO 的getCountWithName(String name) 获取name在数据库中是否存在
long count =customerDAO.getCountWithName(name);
//2.2若返回值大于0, 则响应newcustomer.jsp页面:
//通过转发的方式来响应newcustomer.jsp
if(count>0){
//2.2.1要求在newcustomer.jsp 页面显示一个错误消息:用户名name已经被占用,请重新输入
//在request中放入一个属性message:用户名name已经被占用,请重新输入!
//在页面上通过request.getAttribute("message")的当时来显示
request.setAttribute("message", "用户名"+name+"已经被占用,请重新输入!");
//2.2.2newcustomer.jsp的表单值可以回显。
//通过value="<%= request.getParameter("name") == null ? "" :request.getParameter("name")%>"
//进行回显
//2.2.3结束方法:return
request.getRequestDispatcher("/newcustomer.jsp").forward(request, response);
return;
}
//3.若验证通过,则把表单参数封装为以个Customer对象 customer
Customer customer = new Customer(name,address,phone);
//4.调用CustomerDAO的save(Customer customer)执行保存操作
customerDAO.save(customer);
//5.重定向到success.jsp页面:可以避免表单的重复提交问题
response.sendRedirect("success.jsp");
}
}
SQL:
truncate table customers;会清空数据表,使自增列从1 重新计数
修改操作:
JSP部分:
定义属性
用户名占用提示:
<% Object msg= request.getAttribute("message");
if(msg!=null){
%>
<br>
<font color="red"><%= msg %></font>
<br>
<br>
<%
}
String id=null;
String oldName=null;
String name=null;
String address=null;
String phone=null;
Customer customer=(Customer)request.getAttribute("customer");
if(customer!=null){
id=customer.getId()+"";
oldName=customer.getName();
name=customer.getName();
address=customer.getAddress();
phone=customer.getPhone();
}else{
id=request.getParameter("id");
oldName=request.getParameter("oldName");
name=request.getParameter("oldName");
address=request.getParameter("address");
phone=request.getParameter("phone");
}
隐藏域属性:
<input type="hidden" name="id" value="<%= id%>" />
<input type="hidden" name="oldName" value="<%= oldName %>" />
回显:
若name已存在,则回显oldName和新的address,phone。 通过Ajax会有更好的用户体验
<td><input type="text" name="name" value="<%=name%>" /></td>
Servlet代码:
edit方法:
private void edit(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String forwardPath="/error.jsp";
//1.获取请求参数的id
String idStr=request.getParameter("id");
//2.调用CustomerDAO的customerDAO.get(id)获取和id对应的Customer对象
try {
Customer customer=customerDAO.get(Integer.parseInt(idStr));
if(customer!=null){
forwardPath="/updatecustomer.jsp";
//3.将customer放入request中
request.setAttribute("customer", customer);
}
} catch (NumberFormatException e) {}
//4.响应updatecustomer.jsp页面:转发
request.getRequestDispatcher(forwardPath).forward(request, response);
}
update方法:
private void update(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//1.获取表单参数:id, name,address,phone,oldName
String id=request.getParameter("id");
String name=request.getParameter("name");
String address=request.getParameter("address");
String phone=request.getParameter("phone");
String oldName=request.getParameter("oldName");
//2.检验name 是否已经被占用:
//2.1比较name和oldName是否相同,若相同说明name可用。
//2.1若不相同 ,则调用CustomerDAO 的getCountWithName(String name) 获取name在数据库中是否存在
if(!oldName.equalsIgnoreCase(name)){
long count =customerDAO.getCountWithName(name);
//2.2若返回值大于0, 则响应updatecustomer.jsp页面:
//通过转发的方式来响应updatecustomer.jsp
if(count>0){
//2.2.1要求在updatecustomer.jsp 页面显示一个错误消息:用户名name已经被占用,请重新输入
//在request中放入一个属性message:用户名name已经被占用,请重新输入!
//在页面上通过request.getAttribute("message")的当时来显示
request.setAttribute("message", "用户名"+name+"已经被占用,请重新输入!");
//2.2.2updatecustomer.jsp的表单值可以回显。
//通过value="<%= request.getParameter("name") == null ? "" :request.getParameter("name")%>"
//进行回显 其中address,phone显示提交表单的新的值,而name显示oldName,而不是新提交的name,防止数据丢失
request.getRequestDispatcher("/updatecustomer.jsp").forward(request, response);
//2.2.3结束方法:return
return;
}
}
//3.若验证通过,则把表单参数封装为一个Customer对象 customer
Customer customer = new Customer(name,address,phone);
customer.setId(Integer.parseInt(id));
//4.调用CustomerDAO的update(Customer customer)执行保存操作
customerDAO.update(customer);
//5.重定向到query.do
response.sendRedirect("query.do");
}