1.编程实战(上)
通讯录程序
需求:
添加联系人
修改联系人
删除联系人
查询所有联系人
1.1 需求分析(需求分析师)
a) 功能需求:
需求:
添加联系人
修改联系人
删除联系人
查询所有联系人
b)功能流程
抽取业务模型: 图书模型: 名称,页码,出版社....
功能流程图
1.2 需求设计(系统分析师)
a)美工设计(设计静态原型)
b)系统设计
1)设计实体
class Contact{
String id;
String name;
String gender;
String phone;
String email;
String address;
}
2)设计“数据库”
xml文件代替“数据库”
设计contact.xml
<?xml version="1.0" encoding="UTF-8" ?> <contact-list> <contact> <name>张三</name> <gender>男</gender> <phone>12345678912</phone> <email>zhangsan@qq.com</email> <address>江西南昌</address> </contact> </contact-list>
3)设计接口
面向接口编程:
联系人DAO类:集中编写联系人实体对象的CURD方法。(数据访问对象)
规则: 一个实体对象对应一个dao接口和类
interface ContactDao{
public void addContact(Contact contact);
public void updateContact(Contact contact); 包含修改的id
public void deleteContact(String id);
public List<Contact> findAll();
public Contact findById(String id); 根据id查询对应的联系人
}
4)设计系统目录(包)规范
|-contactSys_web 根目录
|-src
|-gz.itast.contactSys_web.entity 存放实体对象类
|- gz.itast.contactSys_web.dao 存放dao接口和类
|- gz.itast.contactSys_web.web 存放servlet类
|- gz.itast.contactSys_web.util 存放工具类
|- gz.itast.contactSys_web.exception 自定义异常类
|-images 存放图片
|-css 存放css文件
|-js 存放js文件
1.3 编程实现(开发攻城狮)
1)搭建项目环境
a)项目建立
b)建立目录结构
c)导入第三方的包
2)开发步骤:从底层往上层开发
a)“数据库" (设计xml)
b)实体对象(建立实体对象)
c) DAO接口和实现类
d)页面+servlet(跟用户直接相关的)
1.4 功能测试(测试攻城狮)
1.5 性能测试(测试攻城狮)
1.6 部署上线(实施攻城狮)
1.7 系统维护(实施攻城狮)
1)创建包
2)数据
<?xml version="1.0" encoding="UTF-8"?> <contact-list> <contact id="001"> <name>张三</name> <gender>女</gender> <phone>12345678912</phone> <email>zhangsan@qq.com</email> <address>江西南昌</address> </contact> </contact-list>
3)建立实体对象
package cn.jxufe.contactSys_Web.entity; /** * 联系人对象 编号 姓名 性别 电话 邮箱 地址 */ public class Contact { private String id; private String name; private String gender; private String phone; private String email; private String address; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Contact [address=" + address + ", email=" + email + ", gender=" + gender + ", id=" + id + ", name=" + name + ", phone=" + phone + "]"; } }
4)工具类
package cn.jxufe.contactSys_Web.util; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; /** * 把xml文件的通用的操作方法抽取出来 * @author APPle * */ public class XMLUtil { /** * 读取xml文件,返回document对象 */ public static Document getDocument() { try { Document doc = new SAXReader().read(new File("e:/contact.xml")); return doc; } catch (DocumentException e) { e.printStackTrace(); // 把转换为运行时异常抛出即可! throw new RuntimeException(e); } } /** * 传如docuemnt对象,写出到xml文件中 */ public static void write2xml(Document doc) { try { OutputStream out = new FileOutputStream("e:/contact.xml"); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(out, format); writer.write(doc); writer.close(); } catch (Exception e) { e.printStackTrace(); // 把转换为运行时异常抛出即可! throw new RuntimeException(e); } } }
5)Dao接口及实现类
package cn.jxufe.contactSys_Web.dao; import java.util.List; import cn.jxufe.contactSys_Web.entity.Contact; /** * 联系的dao接口 * */ public interface ContactDao { public void addContact(Contact contact); public void updateContact(Contact contact); // 包含修改的id public void deleteContact(String id); public List<Contact> findAll(); public Contact findById(String id); // 根据id查询对应的联系人 }
package cn.jxufe.contactSys_Web.dao.impl; import java.util.ArrayList; import java.util.List; import java.util.UUID; import org.dom4j.Document; import org.dom4j.Element; import org.kohsuke.rngom.digested.Main; import com.sun.mail.handlers.image_gif; import cn.jxufe.contactSys_Web.dao.ContactDao; import cn.jxufe.contactSys_Web.entity.Contact; import cn.jxufe.contactSys_Web.util.XMLUtil; public class ContactDaoImpl implements ContactDao { public void addContact(Contact contact) { try { // 把联系人信息保存到xml文件中去 /** * <contact id="001"> <name>张三</name> <gender>男</gender> <phone>13411112222</phone> <email>zs@qq.com</email> <address>广州天河</address> </contact> */ // 1)读取原来的xml文件,返回document对象 Document doc = XMLUtil.getDocument(); // 2)修改document对象 Element rootElem = doc.getRootElement(); // 添加标签 Element conElem = rootElem.addElement("contact"); // 添加属性 // 编号使用UUID算法生成一个随机且唯一的字符串 conElem.addAttribute("id", UUID.randomUUID().toString()); // 添加子标签 conElem.addElement("name").setText(contact.getName()); conElem.addElement("gender").setText(contact.getGender()); conElem.addElement("phone").setText(contact.getPhone()); conElem.addElement("email").setText(contact.getEmail()); conElem.addElement("address").setText(contact.getAddress()); // 3)把修改后的document对象写出到原来的xml文件中(覆盖原来的xml) XMLUtil.write2xml(doc); } catch (Exception e) { e.printStackTrace(); } } /** * 删除联系人 */ public void deleteContact(String id) { // 1)读取原来的xml Document doc = XMLUtil.getDocument(); // 2)删除contact标签 // 2.1 根据id查询对应的contact标签 Element conElem = (Element) doc.selectSingleNode("//contact[@id='" + id + "']"); if (conElem != null) { // 2.2删除 conElem.detach(); } // 3)把修改的document写出到xml文件 XMLUtil.write2xml(doc); } public List<Contact> findAll() { // 1)读取xml文件 Document doc = XMLUtil.getDocument(); // 2)获取所有的contact标签 List<Element> conList = (List<Element>) doc.getRootElement().elements("contact"); List<Contact> list = new ArrayList<Contact>(); for (Element conElem : conList) { // 2.1 创建Contact对象 Contact contact = new Contact(); // 2.2 把contact标签信息封装到COntact对象中 contact.setId(conElem.attributeValue("id")); contact.setName(conElem.elementText("name")); contact.setGender(conElem.elementText("gender")); contact.setPhone(conElem.elementText("phone")); contact.setEmail(conElem.elementText("email")); contact.setAddress(conElem.elementText("address")); // 2.3 把Contact对象放入List list.add(contact); } return list; } /** * 根据id查询对应的联系人 */ public Contact findById(String id) { // 1)读取xml文件 Document doc = XMLUtil.getDocument(); // 2)查询对应的contact标签 Element conElem = (Element) doc.selectSingleNode("//contact[@id='" + id + "']"); Contact contact = null; if (conElem != null) { // 创建COntact对象 contact = new Contact(); // 把contact标签内容封装到Contact对象中 contact.setId(conElem.attributeValue("id")); contact.setName(conElem.elementText("name")); contact.setGender(conElem.elementText("gender")); contact.setPhone(conElem.elementText("phone")); contact.setEmail(conElem.elementText("email")); contact.setAddress(conElem.elementText("address")); } return contact; } public void updateContact(Contact contact) { try { // 1)读取原理的xml文件 Document doc = XMLUtil.getDocument(); // 2)修改document对象 /** * <contact id="001"> <name>张三</name> <gender>男</gender> <phone>13411112222</phone> <email>zs@qq.com</email> <address>广州天河</address> </contact> */ // 2.1 根据id查询对应的contact标签 Element conElem = (Element) doc.selectSingleNode("//contact[@id='" + contact.getId() + "']"); // 2.2 修改对应的contact标签内容 conElem.element("name").setText(contact.getName()); conElem.element("gender").setText(contact.getGender()); conElem.element("phone").setText(contact.getPhone()); conElem.element("email").setText(contact.getEmail()); conElem.element("address").setText(contact.getAddress()); // 3)修改的document对象写出xml文件中 XMLUtil.write2xml(doc); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { ContactDao dao = new ContactDaoImpl(); List<Contact> conList = dao.findAll(); for (Contact contact : conList) { System.out.println(contact); } } }
6)页面+servlet(跟用户直接相关的)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>修改联系人</title> </head> <body> <h3 style="text-align: center">修改联系人</h3> <form action="查询联系人.html" method="post"> <table align="center" border="1"> <tr> <th>姓名</th> <td><input type="text" name="name" value="张三"/></td> </tr> <tr> <th>性别</th> <td><input type="radio" name="gender" value="男" checked="checked"/>男 <input type="radio" name="gender" value="女" />女 </td> </tr> <tr> <th>电话</th> <td><input type="text" name="phone" value="12345678912"/></td> </tr> <tr> <th>邮箱</th> <td><input type="text" name="email" value="zhangsan@qq.com"/></td> </tr> <tr> <th>地址</th> <td><input type="text" name="address" value="江西南昌"/></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="添加"></td> </tr> </table> </form> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>查询联系人</title> </head> <body> <h3 style="text-align: center">查询所有联系人</h3> <table border="1" align="center" width="700px"> <tr> <th>编号</th> <th>姓名</th> <th>性别</th> <th>电话</th> <th>邮箱</th> <th>地址</th> <th>操作</th> </tr> <tr> <td>1</td> <td>张三</td> <td>男</td> <td>12345678911</td> <td>zhangsan@qq.com</td> <td>江西南昌</td> <td><a href="修改联系人.html">修改</a> <a href="">删除</a></td> </tr> <tr> <td colspan="7" align="center"><a href="添加联系人.html">添加联系人</a> </td> </tr> </table> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加联系人</title> </head> <body> <h3 style="text-align: center">添加联系人</h3> <form action="查询联系人.html" method="post"> <table align="center" border="1"> <tr> <th>姓名</th> <td><input type="text" name="name" placeholder="请输入姓名"/></td> </tr> <tr> <th>性别</th> <td><input type="radio" name="gender" value="男" placeholder="请选择您的性别"/>男 <input type="radio" name="gender" value="女" placeholder="请选择您的性别"/>女 </td> </tr> <tr> <th>电话</th> <td><input type="text" name="phone" placeholder="请输入电话号码"/></td> </tr> <tr> <th>邮箱</th> <td><input type="text" name="email" placeholder="请输入邮箱"/></td> </tr> <tr> <th>地址</th> <td><input type="text" name="address" placeholder="请输入地址"/></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="添加"></td> </tr> </table> </form> </body> </html>
package cn.jxufe.contactSys_Web.web; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.jxufe.contactSys_Web.dao.ContactDao; import cn.jxufe.contactSys_Web.dao.impl.ContactDaoImpl; import cn.jxufe.contactSys_Web.entity.Contact; public class ConListServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1)读取所有联系人数据 ContactDao dao = new ContactDaoImpl(); List<Contact> conList = dao.findAll(); // 2)显示到浏览器 response.setContentType("text/html;charset=utf-8"); String html = ""; html += "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> "; html += "<html xmlns='http://www.w3.org/1999/xhtml'> "; html += "<head> "; html += "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> "; html += "<title>查询所有联系人</title> "; html += "</head> "; html += " "; html += "<body> "; html += "<center><h3>查询所有联系人</h3></center> "; html += "<table border='1' align='center' width='700px'> "; html += " <tr> "; html += " <th>编号</th> "; html += " <th>姓名</th> "; html += " <th>性别</th> "; html += " <th>电话</th> "; html += " <th>邮箱</th> "; html += " <th>地址</th> "; html += " <th>操作</th> "; html += " </tr> " + " "; if (conList != null) { for (Contact contact : conList) { html += " <tr> "; html += " <td>" + contact.getId() + "</td> "; html += " <td>" + contact.getName() + "</td> "; html += " <td>" + contact.getGender() + "</td> "; html += " <td>" + contact.getPhone() + "</td> "; html += " <td>" + contact.getEmail() + "</td> "; html += " <td>" + contact.getAddress() + "</td> "; html += " <td><a href='" + request.getContextPath() + "/ConQueryServlet?id=" + contact.getId() + "'>修改</a> <a href='" + request.getContextPath() + "/ConDelServlet?id=" + contact.getId() + "'>删除</a></td> "; html += " </tr> "; } } html += " <tr> "; html += " <td colspan='7' align='center'><a href='" + request.getContextPath() + "/addCon.html'>【添加联系人】</a></td> "; html += " </tr> "; html += "</table> "; html += "</body> "; html += "</html> "; response.getWriter().write(html); } }
package cn.jxufe.contactSys_Web.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.jxufe.contactSys_Web.dao.ContactDao; import cn.jxufe.contactSys_Web.dao.impl.ContactDaoImpl; import cn.jxufe.contactSys_Web.entity.Contact; /* * 添加联系人的servlet */ public class ConAddServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 解决post提交数据的中文乱码 request.setCharacterEncoding("utf-8"); // 1)接收表单的数据 String name = request.getParameter("name"); String gender = request.getParameter("gender"); String phone = request.getParameter("phone"); String email = request.getParameter("email"); String address = request.getParameter("address"); // 把数据封装到Contact对象 Contact contact = new Contact(); contact.setName(name); contact.setGender(gender); contact.setEmail(email); contact.setPhone(phone); contact.setAddress(address); // 2)把数据保存到xml中 ContactDao dao = new ContactDaoImpl(); dao.addContact(contact); // 3)跳转到查询联系人页面 // 重定向 response.sendRedirect(request.getContextPath() + "/ConListServlet"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package cn.jxufe.contactSys_Web.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.jxufe.contactSys_Web.dao.ContactDao; import cn.jxufe.contactSys_Web.dao.impl.ContactDaoImpl; /* * 删除联系人的servlet */ public class ConDelServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1)接收需要删除的id String id = request.getParameter("id"); // 2)在xml文件中删除联系人 ContactDao dao = new ContactDaoImpl(); dao.deleteContact(id); // 3)跳转到查询联系人页面 // 重定向 response.sendRedirect(request.getContextPath() + "/ConListServlet"); } // public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // // } }
package cn.jxufe.contactSys_Web.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.jxufe.contactSys_Web.dao.ContactDao; import cn.jxufe.contactSys_Web.dao.impl.ContactDaoImpl; import cn.jxufe.contactSys_Web.entity.Contact; /* * 查询联系人的Servlet */ public class ConQueryServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1)接收需要修改的id String id = request.getParameter("id"); // 2)查询对应的联系人 ContactDao dao = new ContactDaoImpl(); Contact contact = dao.findById(id); // 3)显示到浏览器 response.setContentType("text/html;charset=utf-8"); String html = ""; html += "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> "; html += "<html xmlns='http://www.w3.org/1999/xhtml'> "; html += "<head> "; html += "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> "; html += "<title>修改联系人</title> "; html += "</head> "; html += " "; html += "<body> "; html += "<center><h3>修改联系人</h3></center> "; html += "<form action='" + request.getContextPath() + "/ConUpdateServlet' method='post'> "; // 使用隐藏域,用于传递参数,而这个参数用户可以不用看见 html += "<input type='hidden' name='id' value='" + contact.getId() + "'/>"; html += "<table border='1' align='center' width='300px'> "; html += " <tr> "; html += " <th>姓名</th> "; html += " <td><input type='text' name='name' value='" + contact.getName() + "'/></td> "; html += " </tr> "; html += " <tr> "; html += " <th>性别</th> "; html += " <td> "; if (contact.getGender().equals("男")) { html += " <input type='radio' name='gender' value='男' checked='checked'/>男 "; html += " <input type='radio' name='gender' value='女'/>女 "; } else { html += " <input type='radio' name='gender' value='男' />男 "; html += " <input type='radio' name='gender' value='女' checked='checked'/>女 "; } html += " </td> "; html += " </tr> "; html += " <tr> "; html += " <th>电话</th> "; html += " <td><input type='text' name='phone' value='" + contact.getPhone() + "'/></td> "; html += " </tr> "; html += " <tr> "; html += " <th>邮箱</th> "; html += " <td><input type='text' name='email' value='" + contact.getEmail() + "'/></td> "; html += " </tr> "; html += " <tr> "; html += " <th>地址</th> "; html += " <td><input type='text' name='address' value='" + contact.getAddress() + "'/></td> "; html += " </tr> "; html += " <tr> "; html += " <td colspan='2' align='center'><input type='submit' value='修改'/></td> "; html += " </tr> "; html += "</table> "; html += "</form> "; html += "</body> "; html += "</html> "; response.getWriter().write(html); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package cn.jxufe.contactSys_Web.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.jxufe.contactSys_Web.dao.ContactDao; import cn.jxufe.contactSys_Web.dao.impl.ContactDaoImpl; import cn.jxufe.contactSys_Web.entity.Contact; /* * 修改联系人的servlet */ public class ConUpdateServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); // 1)接收表单的数据 String name = request.getParameter("name"); String gender = request.getParameter("gender"); String phone = request.getParameter("phone"); String email = request.getParameter("email"); String address = request.getParameter("address"); String id = request.getParameter("id"); // 把数据封装到Contact对象 Contact contact = new Contact(); contact.setName(name); contact.setGender(gender); contact.setEmail(email); contact.setPhone(phone); contact.setAddress(address); contact.setId(id); // 2)把数据保存到xml ContactDao dao = new ContactDaoImpl(); dao.updateContact(contact); // 3)跳转到查询联系人页面 response.sendRedirect(request.getContextPath() + "/ConListServlet"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }