寒假软件03

今天:实现增加,修改


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.itheima.domain.Cust;
import com.itheima.factory.BasicFactory;
import com.itheima.service.CustService;

public class AddCustServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        CustService service = BasicFactory.getFactory().getInstance(CustService.class);
        try{

            Cust cust = new Cust();
            BeanUtils.populate(cust, request.getParameterMap());

            String [] prefs = request.getParameterValues("preference");
            StringBuffer buffer = new StringBuffer();
            for(String pref : prefs){
                buffer.append(pref+",");
            }
            String pref = buffer.substring(0, buffer.length()-1);
            cust.setPreference(pref);

            service.addCust(cust);
            response.sendRedirect(request.getContextPath()+"/index.jsp");
        }catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.itheima.domain.Cust;
import com.itheima.factory.BasicFactory;
import com.itheima.service.CustService;

public class UpdateCustServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        CustService service = BasicFactory.getFactory().getInstance(CustService.class);
        try{
            Cust cust = new Cust();
            BeanUtils.populate(cust, request.getParameterMap());
            String [] prefs = request.getParameterValues("preference");
            StringBuffer buffer = new StringBuffer();
            for(String pref : prefs){
                buffer.append(pref+",");
            }
            String pref = buffer.substring(0, buffer.length()-1);
            cust.setPreference(pref);
            service.updateCust(cust);
            request.getRequestDispatcher("/servlet/ListCustServlet").forward(request, response);
        }catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body style="text-align: center;">
      <h1>客户管理系统_修改客户</h1><hr>
      <form action="${pageContext.request.contextPath }/servlet/UpdateCustServlet" method="POST">
          <input type="hidden" name="id" value="${cust.id }">
          <table border="1">
              <tr>
                  <td>客户姓名:</td>
                  <td><input type="text" name="name" value="${cust.name }" readonly="readonly" style="background: silver"/></td>
              </tr>
              <tr>
                  <td>客户性别:</td>
                  <td>
                      <input type="radio" name="gender" value="男"
                          <c:if test="${cust.gender=='男' }">checked='checked'</c:if>
                      />男
                      <input type="radio" name="gender" value="女"
                          <c:if test="${cust.gender=='女' }">checked='checked'</c:if>
                      />女
                  </td>
              </tr>
              <tr>
                  <td>出生日期:</td>
                  <td><input type="text" name="birthday" value="${cust.birthday }" /></td>
              </tr>
              <tr>
                  <td>手机号码:</td>
                  <td><input type="text" name="cellphone" value="${cust.cellphone }"  /></td>
              </tr>
              <tr>
                  <td>电子邮箱:</td>
                  <td><input type="text" name="email" value="${cust.email }" /></td>
              </tr>
              <tr>
                  <td>客户爱好:</td>
                  <td>
                      <input type="checkbox" name="preference" value="篮球" 
                          
                      />篮球
                      <input type="checkbox" name="preference" value="乒乓球" 
                          
                      />乒乓球
                      <input type="checkbox" name="preference" value="玻璃球"
                          
                       />玻璃球
                      <input type="checkbox" name="preference" value="铅球"
                          
                       />铅球
                  </td>
              </tr>
              <tr>
                  <td>客户类型:</td>
                  <td>
                    <select name="type">
                        <option value="钻石客户"
                            <c:if test="${cust.type=='钻石客户' }">
                                 selected="selected"
                            </c:if>
                        >钻石客户</option>
                        <option value="白金客户"
                            <c:if test="${cust.type=='白金客户' }">
                                 selected="selected"
                            </c:if>
                        >白金客户</option>
                        <option value="黄金客户"
                            <c:if test="${cust.type=='黄金客户' }">
                                 selected="selected"
                            </c:if>
                        >黄金客户</option>
                        <option value="白银客户"
                            <c:if test="${cust.type=='白银客户' }">
                                 selected="selected"
                            </c:if>
                        >白银客户</option>
                        <option value="青铜客户"
                            <c:if test="${cust.type=='青铜客户' }">
                                 selected="selected"
                            </c:if>
                        >青铜客户</option>
                        <option value="黑铁客户"
                            <c:if test="${cust.type=='黑铁客户' }">
                                 selected="selected"
                            </c:if>
                        >黑铁客户</option>
                        <option value="没牌客户"
                            <c:if test="${cust.type=='没牌客户' }">
                                 selected="selected"
                            </c:if>
                        >没牌客户</option>
                    </select>
                </td>
              </tr>
              <tr>
                  <td>描述信息:</td>
                  <td><textarea name="description" rows="6" cols="40" >${cust.description }</textarea></td>
              </tr>
              <tr>
                  <td colspan="2">
                      <input type="submit" value="修改客户"/>
                  </td>
              </tr>
          </table>
      </form>
  </body>
</html>

 明天:实现删除,批量删除

问题:无

posted on 2018-02-25 11:44  王行行  阅读(153)  评论(0)    收藏  举报

导航