1 需求分析(需求分析师)

功能分析:

1)添加联系人

2)修改联系人

3)删除联系人

4)查询所有联系人

2 需求设计(系统分析师/架构师/资深开发人员)

2.1设计实体(抽象实体)

联系人实体:

class Contact{
private String id;
private String name;
private String gender;
private int age;
private String phone;
private String email;
private String qq;
  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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
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 getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
@Override
public String toString() {
return "Contact [age=" + age + ", email=" + email + ", gender="
+ gender + ", id=" + id + ", name=" + name + ", phone=" + phone
+ ", qq=" + qq + "]";
}
}

2.2设计“数据库”,(xml代替"数据库")

XML格式

contact.xml
<contactList>
<contact id="1">
<name>张三</name>
<gender>男</gender>
<age>20</age>
<phone>13433334444</phone>
<email>zs@qq.com</email>
<qq>43222222<qq>
</contact>
</contactList>

XML工具类

 public class XMLUtil {

     /**
* 读取xml文档方法
* @return
*/
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);
}
} /**
* 写出到xml文档中
*/
public static void write2xml(Document doc){
try {
FileOutputStream out = new FileOutputStream("e:/contact.xml");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter writer = new XMLWriter(out,format);
writer.write(doc);
writer.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}

2.3设计涉及的接口

DAO接口(数据访问对象):实体对象的CRUD方法。

项目原则: 通常一个实体对象就会对应一个DAO接口和一个DAO实现类

DAO接口

interface ContactDao{
public void addContact(Contact contact);//添加联系人
public void updateContact(Contact contact);//修改联系人
public void deleteContact(String id);//删除联系人
public List<Contact> findAll(); //查询所有联系人
public Contact findById(String id);//根据编号查询联系人
}

DAO实现类

 public class ContactDaoImpl implements ContactDao {

     /**
* 添加联系人
*/
public void addContact(Contact contact) {
try {
File file = new File("e:/contact.xml");
Document doc = null;
Element rootElem = null;
if(!file.exists()){
/**
* 需求: 把contact对象保存到xml文件中
*/
//如果没有xml文件,则创建xml文件
doc = DocumentHelper.createDocument();
//创建根标签
rootElem = doc.addElement("contactList");
}else{
//如果有xml文件,则读取xml文件
doc = XMLUtil.getDocument();
//如果有xml文件,读取根标签
rootElem = doc.getRootElement();
} //添加contact标签
/**
* <contact id="1">
<name>eric</name>
<gender>男</gender>
<age>20</age>
<phone>1343333</phone>
<email>eric@qq.com</email>
<qq>554444</qq>
</contact>
*/
Element contactElem = rootElem.addElement("contact"); /**
* 由系统自动生成随机且唯一的ID值,赋值给联系人
*/
String uuid = UUID.randomUUID().toString().replace("-",""); contactElem.addAttribute("id", uuid);
contactElem.addElement("name").setText(contact.getName());
contactElem.addElement("gender").setText(contact.getGender());
contactElem.addElement("age").setText(contact.getAge()+"");
contactElem.addElement("phone").setText(contact.getPhone());
contactElem.addElement("email").setText(contact.getEmail());
contactElem.addElement("qq").setText(contact.getQq());
//把Document写出到xml文件
XMLUtil.write2xml(doc);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} /**
* 删除联系人
*/
public void deleteContact(String id) {
try {
//1.读取xml文件
Document doc = XMLUtil.getDocument();
//2.查询需要删除id的contact
Element contactElem = (Element)doc.selectSingleNode("//contact[@id='"+id+"']");
//删除标签
if(contactElem!=null){
contactElem.detach();
} //3.把Document写出到xml文件
XMLUtil.write2xml(doc);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} /**
* 查询所有联系人
*/
public List<Contact> findAll() {
//1.读取xml文件
Document doc = XMLUtil.getDocument(); //2.创建List对象
List<Contact> list = new ArrayList<Contact>();
//3.读取contact标签
List<Element> conList = (List<Element>)doc.selectNodes("//contact");
for(Element e:conList){
//创建COntact对象
Contact c = new Contact();
c.setId(e.attributeValue("id"));
c.setName(e.elementText("name"));
c.setGender(e.elementText("gender"));
c.setAge(Integer.parseInt(e.elementText("age")));
c.setPhone(e.elementText("phone"));
c.setEmail(e.elementText("email"));
c.setQq(e.elementText("qq"));
//把Contact放入list中
list.add(c);
}
return list;
} /**
* 根据编号查询联系人
*/
public Contact findById(String id) {
Document doc = XMLUtil.getDocument();
Element e = (Element)doc.selectSingleNode("//contact[@id='"+id+"']"); Contact c = null;
if(e!=null){
//创建COntact对象
c = new Contact();
c.setId(e.attributeValue("id"));
c.setName(e.elementText("name"));
c.setGender(e.elementText("gender"));
c.setAge(Integer.parseInt(e.elementText("age")));
c.setPhone(e.elementText("phone"));
c.setEmail(e.elementText("email"));
c.setQq(e.elementText("qq"));
}
return c;
} /**
* 修改联系人
*/
public void updateContact(Contact contact) {
/**
* 需求: 修改id值为2的联系人
* 1)查询id值为2的contact标签
* 2)修改contact标签的内容
*/
try {
//1.读取xml文件
Document doc = XMLUtil.getDocument(); Element contactElem = (Element)doc.selectSingleNode("//contact[@id='"+contact.getId()+"']"); //2.修改contact标签内容 contactElem.element("name").setText(contact.getName());
contactElem.element("gender").setText(contact.getGender());
contactElem.element("age").setText(contact.getAge()+"");
contactElem.element("phone").setText(contact.getPhone());
contactElem.element("email").setText(contact.getEmail());
contactElem.element("qq").setText(contact.getQq());
//3.把Document写出到xml文件
XMLUtil.write2xml(doc);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
//测试UUID
String uuid = UUID.randomUUID().toString().replace("-","");
System.out.println(uuid);
} }

2.4设计项目的目录结构

项目名称: contactSys_web

目录结构:

|- contactSys_web

  |-src

    |-gz.itcast.contactSys_web.entity : 存放实体对象

    |-gz.itcast.contactSys_web.dao : 存放dao的接口

    |-gz.itcast.contactSys_web.dao.impl: 存放dao的实现类

    |-gz.itcast.contactSys_web.servlet: 存放servlet的类

    |-gz.itcast.contactSys_web.test: 存放单元测试类

    |-gz.itcast.contactSys_web.util: 存放工具类

    |-gz.itcast.contactSys_web.exception: 存放自定义异常类

  |-WebRoot

    |-html文件

    |-images:目录。存放图片资源

    |-css:目录。存放css资源

    |-js:目录。存放js资源

3 编码实现(软件开发工程师/攻城狮)

开发顺序:

设计数据库-> 实体 -> DAO接口,DAO实现-> Servlet+html页面

3.1显示所有人的逻辑

public class ListContactServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.从xml中读取出联系人数据
ContactDao dao = new ContactDaoImpl();
List<Contact> list = dao.findAll(); //2.显示到浏览器
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter(); String html = ""; //shift+alt+A ^(.*)$ \1";
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 += "<style type='text/css'>";
html += " table td{";
html += " /*文字居中*/";
html += " text-align:center;";
html += " }";
html += " ";
html += " /*合并表格的边框*/";
html += " table{";
html += " border-collapse:collapse;";
html += " }";
html += "</style>";
html += "</head>";
html += "";
html += "<body>";
html += "<center><h3>查询所有联系人</h3></center>";
html += "<table align='center' border='1' width='800px'>";
html += " <tr>";
html += " <th>编号</th>";
html += " <th>姓名</th>";
html += " <th>性别</th>";
html += " <th>年龄</th>";
html += " <th>电话</th>";
html += " <th>邮箱</th>";
html += " <th>QQ</th>";
html += " <th>操作</th>";
html += " </tr>";
if(list!=null){
for (Contact contact : list) {
html += " <tr>";
html += " <td>"+contact.getId()+"</td>";
html += " <td>"+contact.getName()+"</td>";
html += " <td>"+contact.getGender()+"</td>";
html += " <td>"+contact.getAge()+"</td>";
html += " <td>"+contact.getPhone()+"</td>";
html += " <td>"+contact.getEmail()+"</td>";
html += " <td>"+contact.getQq()+"</td>";
html += " <td><a href='"+request.getContextPath()+"/QueryContactServlet?id="+contact.getId()+"'>修改</a>&nbsp;<a href='"+request.getContextPath()+"/DeleteContactServlet?id="+contact.getId()+"'>删除</a></td>";
html += " </tr>";
}
}
html += " <tr>";
html += " <td colspan='8' align='center'><a href='"+request.getContextPath()+"/addContact.html'>[添加联系人]</a></td>";
html += " </tr>";
html += "</table>";
html += "</body>";
html += "</html>"; writer.write(html); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }

3.2添加联系人的逻辑

 public class AddContactServlet 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 age = request.getParameter("age");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
String qq = request.getParameter("qq"); //封装成Contact对象
Contact contact = new Contact();
contact.setName(name);
contact.setGender(gender);
contact.setAge(Integer.parseInt(age));
contact.setPhone(phone);
contact.setEmail(email);
contact.setQq(qq); //2.调用dao类的添加联系人的方法
ContactDao dao = new ContactDaoImpl();
dao.addContact(contact); //3.跳转到查询联系人的页面
response.sendRedirect(request.getContextPath()+"/ListContactServlet");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }

3.3修改前查询联系人的逻辑

 public class QueryContactServlet extends HttpServlet {

     public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.接收id
String id = request.getParameter("id"); //2.调用dao根据id查询联系人的方法
ContactDao dao = new ContactDaoImpl();
Contact contact = dao.findById(id); //3.把联系人显示到浏览器中
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter(); 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()+"/UpdateContactServlet' method='post'>";
//注意:添加id的隐藏域
html += "<input type='hidden' name='id' value='"+contact.getId()+"'/>";
html += "<table align='center' border='1' 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 if(contact.getGender().equals("女")){
html += " <input type='radio' name='gender' value='男'/>男";
html += " <input type='radio' name='gender' value='女' checked='checked'/>女";
}else{
html += " <input type='radio' name='gender' value='男' checked='checked'/>男";
html += " <input type='radio' name='gender' value='女'/>女";
} html += " </td>";
html += " </tr>";
html += " <tr>";
html += " <th>年龄</th>";
html += " <td><input type='text' name='age' value='"+contact.getAge()+"'/></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>QQ</th>";
html += " <td><input type='text' name='qq' value='"+contact.getQq()+"'/></td>";
html += " </tr>";
html += " <tr>";
html += " <td colspan='2' align='center'>";
html += " <input type='submit' value='保存'/>&nbsp;";
html += " <input type='reset' value='重置'/></td>";
html += " </tr>";
html += "</table>";
html += "</form>";
html += "</body>";
html += "</html>"; writer.write(html);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }

3.4修改联系人的逻辑

 public class UpdateContactServlet extends HttpServlet {

     public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//1.接收参数
String id = request.getParameter("id");
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String age = request.getParameter("age");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
String qq = request.getParameter("qq"); //封装成Contact对象
Contact contact = new Contact();
contact.setId(id);
contact.setName(name);
contact.setGender(gender);
contact.setAge(Integer.parseInt(age));
contact.setPhone(phone);
contact.setEmail(email);
contact.setQq(qq); //2.调用dao修改联系人的方法
ContactDao dao = new ContactDaoImpl();
dao.updateContact(contact); //3.跳转到查询联系人的页面
response.sendRedirect(request.getContextPath()+"/ListContactServlet"); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }

3.5删除联系人的逻辑

 public class DeleteContactServlet extends HttpServlet {

     public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//在火狐浏览器中以Get方式提交带参数的数据,会重复提交两次。
System.out.println("删除联系人");
//1.接收id
String id = request.getParameter("id"); //2.调用dao删除联系人的方法
ContactDao dao = new ContactDaoImpl();
dao.deleteContact(id); //3.跳转到查询联系人的页面
response.sendRedirect(request.getContextPath()+"/ListContactServlet");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }

4 功能测试(测试攻城狮)

5 性能测试(测试攻城狮)

6 部署上线(实施攻城狮)

7 维护阶段(实施攻城狮)

<项目><day12>通讯录(视频)的更多相关文章

  1. <项目><day12>通讯录(自己做的)

    设计一个通讯录主页面 <!DOCTYPE html> <html> <head> <title>电话本首页</title> <meta ...

  2. Asp.NET Core2.0 项目实战入门视频课程_完整版

    END OR START? 看到这个标题,你开不开心,激不激动呢? 没错,.net core的入门课程已经完毕了.52ABP.School项目从11月19日,第一章视频的试录制,到今天完整版出炉,离不 ...

  3. 给tomcat配置外部资源路径(应用场景:web项目访问图片视频等资源)

    对于一个web项目来说,除了文字之外,图片,视频等媒体元素也是其重要的组成部分.我们知道,web项目中如果用到大量的图片.视屏的资源,我们 通常的做法是只在数据库中存储图片.视频等资源的路径,web项 ...

  4. 0506-Scrum 项目 2.0视频

    一.团队项目要求 应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 录制为演说视频,上传到视频网站,并把链接发到团队博客上. 二.NABCD模型 选题:约拍平台——家教平台 1) ...

  5. 优秀开源项目之一:视频监控系统iSpy

    iSpy是一个开源的视频监控软件,目前已经支持中文.自己用了一下,感觉还是很好用的.翻译了一下它的介绍. iSpy将PC变成一个完整的安全和监控系统 iSpy使用您的摄像头和麦克风来检测和记录声音或运 ...

  6. 【VIP视频网站项目】VIP视频网站项目v1.0.3版本发布啦(程序一键安装+电影后台自动抓取+代码结构调整)

    在线体验地址:http://vip.52tech.tech/ GIthub源码:https://github.com/xiugangzhang/vip.github.io 项目预览 主页面 登录页面 ...

  7. 实战项目:通讯录&nbsp;UI—第十一天

     1.推出视图的两种方式:  1.通过导航控制器push到下一个界面,使用pop返回到上一个界面 2.通过模态的形式推出视图,不需要依赖于导航控制器,通过使用present到下一个界面,通过dismi ...

  8. python项目开发视频

    精品Python项目开发学习视频 所属网站分类: 资源下载 > python视频教程 作者:乐天派 链接:http://www.pythonheidong.com/blog/article/44 ...

  9. 【原创】基于NodeJS Express框架开发的一个VIP视频网站项目及源码分享

    项目名称:视频网站项目 开发语言:HTML,CSS(前端),JavaScript,NODEJS(expres)(后台) 数据库:MySQL 开发环境:Win7,Webstorm 上线部署环境:Linu ...

随机推荐

  1. NIO入门之轻松读取大文件

    NIO入门之轻松读取大文件 今天同事碰到了一个问题,从游戏服务器下载下来的输出log有一个多G大.用记事本打不开,EditPlus也打不开,都提示文件太大.用word也打不开,提示文件大于512M.打 ...

  2. ABAP的HTTP_GET和Linux的curl

    curl是利用URL语法在命令行方式下工作的开源文件传输工具,广泛应用在Unix,多种Linux发行版中. 在Windows系统下也有移植版. curl尤其被广泛应用在github上众多开源软件和框架 ...

  3. jquery.placeholder.min.js让吃屎的IE浏览器支持placeholder去吧

    描述:现在都是HTML5时代了,所有的浏览器都支持placeholder,唯独IE不支持.现在我们有了这款插件,IE下终于可以支持了!  图片展示:   兼容浏览器:IE6+/Firefox/Goog ...

  4. T1订正记-AC自动机-从树到图

    AC自动机已经足够棒了. 但是,好像有时还是要TLE的. 一般的AC自动还是比较好,如果在某些情况下还是会被卡掉,像是这个水题 考试的感觉 我看到这个题后,我清清楚楚的知道,这是个AC自动机+栈. 经 ...

  5. mean shift博客推荐

    https://blog.csdn.net/maweifei/article/details/78766784 https://blog.csdn.net/gdfsg/article/details/ ...

  6. 什么是session?

    Session一般译作会话.从不同的层面看待session,它有着类似但不全然相同的含义.比如,在web应用的用户看来,他打开浏览器访问一个电子商务网站,登录.并完成购物直到关闭浏览器,这是一个会话. ...

  7. [CF] 986 A. Fair

    http://codeforces.com/problemset/problem/986/A n个点的无向连通图,每个点有一个属性,求每个点到s个不同属性点的最短距离 依稀记得那天晚上我和Menteu ...

  8. 10. InnoDB表空间加密

    10. InnoDB表空间加密 InnoDB支持存储在单独表空间中的表的数据加密 .此功能为物理表空间数据文件提供静态加密. 详细信息见官方文档

  9. docker:安装tomcat

    文章来源:http://www.cnblogs.com/hello-tl/p/8929879.html 0.下载镜像 # docker pull tomcat:8.5 1.复制tomcat配置 先启动 ...

  10. Python旅途——简单语法

    1. 前言 在我们对环境以及pycharm安装好之后,我们就可以开始我们的Python之旅了,那么,我们学习一门语言应该如何开始呢?就像我们学习汉语一样,从abcd这些拼音学起,而对于我们Python ...