通讯录(自己做的)
1.添加联系人
1.1保存页面
1.2保存逻辑
2.修改联系人
2.1修改联系人页面
2.2修改联系人逻辑
3.删除联系人
4.显示联系人列表
设计一个通讯录主页面
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>电话本首页</title> 5 6 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 7 <meta http-equiv="description" content="this is my page"> 8 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 9 10 <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> 11 12 </head> 13 14 <body> 15 <table align="center" border="1"> 16 <form> 17 <center><h1>电话簿</h1></center> 18 <tr> 19 <td>1.<a href="/myPhoneBook/PhoneList"><input type="button" value="查询联系人"/></a></td> 20 </tr> 21 <tr> 22 <td>2.<a href="add.html"><input type="button" value="添加联系人"/></a></td> 23 </tr> 24 <tr> 25 <td>3.<a href="update.html"><input type="button" value="修改联系人"/></a></td> 26 </tr> 27 <tr> 28 <td>4.<a href="/myPhoneBook/Delete"><input type="button" value="删除联系人"/></a></td> 29 </tr> 30 31 </form> 32 </table> 33 </body> 34 </html>
1.添加联系人:
1.1保存页面
<!DOCTYPE html> <html> <head> <title>添加联系人页面</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <form action="/myPhoneBook/Add" method="POST"> <table cellpadding="10" border="1" align="center"> <tr> <td>编号</td> <td><input type="text" name="id"/></td> </tr> <tr> <td>姓名</td> <td><input type="text" name="name"/></td> </tr> <tr> <td>年龄</td> <td><input type="text" name="age"/></td> </tr> <tr> <td>性别</td> <td> 男<input type="radio" checked="checked" name="gender" value="男"/> 女<input type="radio" name="gender" value="女"/> </td> </tr> <tr> <td>电话</td> <td><input type="text" name="phone"/></td> </tr> <tr> <td>QQ</td> <td><input type="text" name="qq"/></td> </tr> <tr> <td>邮箱</td> <td><input type="text" name="mail"/></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="添加联系人"/><a href="index.html"></a></td> </tr> </table> </form> </body> </html>
1.2保存逻辑:
AddServlet (接收页面数据(getParameter()),使用dom4j保存到xml)修改联系人(完成)
1 public class Add extends HttpServlet { 2 3 public void doPost(HttpServletRequest request, HttpServletResponse response) 4 throws ServletException, IOException { 5 6 request.setCharacterEncoding("utf-8"); 7 response.setContentType("text/html;charset=utf-8"); 8 9 String id = request.getParameter("id"); 10 String name = request.getParameter("name"); 11 String age = request.getParameter("age"); 12 String gender = request.getParameter("gender"); 13 String phone = request.getParameter("phone"); 14 String qq = request.getParameter("qq"); 15 String mail = request.getParameter("mail"); 16 17 try { 18 write(id,name,age,gender,phone,qq,mail); 19 } catch (DocumentException e) { 20 e.printStackTrace(); 21 } 22 23 response.getWriter().write("<font color=\"red\" size=\"6px\">添加成功</font><a href=\"index.html\">返回首页</a>"); 24 } 25 26 public void write(String id, String name, String age, String gender, 27 String phone, String qq, String mail) throws IOException, DocumentException { 28 //关联或者创建一个xml文档 29 // File file = new File("h:/testdemo/jiuye/myPhoneBook/Person.xml"); 30 File file = new File("/Person.xml"); 31 //判断xml文档是否存在 32 if(!file.exists()){ 33 //不存在,创建一个文档 ,并且添加根标签 34 Document doc = DocumentHelper.createDocument(); 35 Element rootElem = doc.addElement("Persons"); 36 save(file,doc); 37 } 38 //存在,读取这个文档根标签 39 Document doc = new SAXReader().read(file); 40 Element rootElem = doc.getRootElement(); 41 42 Element perElem = rootElem.addElement("person"); 43 44 perElem.addElement("id").setText(id); 45 46 perElem.addElement("name").setText(name); 47 48 perElem.addElement("age").setText(age); 49 50 perElem.addElement("gender").setText(gender); 51 52 perElem.addElement("phone").setText(phone); 53 54 perElem.addElement("qq").setText(qq); 55 56 perElem.addElement("mail").setText(mail); 57 save(file, doc); 58 } 59 60 public void save(File file, Document doc) throws IOException { 61 //将Document对象写入xml文档 62 FileOutputStream out = new FileOutputStream(file); 63 OutputFormat format = OutputFormat.createPrettyPrint(); 64 format.setEncoding("utf-8"); 65 XMLWriter writer = new XMLWriter(out,format); 66 writer.write(doc); 67 writer.close(); 68 } 69 70 }
2.修改联系人
2.1修改页面
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>修改联系人页面</title> 5 6 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 7 <meta http-equiv="description" content="this is my page"> 8 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 9 10 <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> 11 12 </head> 13 14 <body> 15 <form action="/myPhoneBook/Update" method="POST"> 16 <center><h1>修改联系人</h1></center> 17 <table cellpadding="10" border="1" align="center"> 18 <tr> 19 <td>需要修改人编号</td> 20 <td><input type="text" name="id_1"/></td> 21 </tr> 22 <tr> 23 <td>编号</td> 24 <td><input type="text" name="id"/></td> 25 </tr> 26 <tr> 27 <td>姓名</td> 28 <td><input type="text" name="name"/></td> 29 </tr> 30 <tr> 31 <td>年龄</td> 32 <td><input type="text" name="age"/></td> 33 </tr> 34 <tr> 35 <td>性别</td> 36 <td> 37 男<input type="radio" checked="checked" name="gender" value="男"/> 38 女<input type="radio" name="gender" value="女"/> 39 </td> 40 </tr> 41 <tr> 42 <td>电话</td> 43 <td><input type="text" name="phone"/></td> 44 </tr> 45 <tr> 46 <td>QQ</td> 47 <td><input type="text" name="qq"/></td> 48 </tr> 49 <tr> 50 <td>邮箱</td> 51 <td><input type="text" name="mail"/></td> 52 </tr> 53 <tr> 54 <td colspan="2" align="center"><input type="submit" value="确认修改"/></td> 55 </tr> 56 </table> 57 </form> 58 </body> 59 </html>
2.2修改逻辑
UpdateServlet ( 接收页面数据,把数据保存xml )
1 public class Update extends HttpServlet { 2 3 public void doPost(HttpServletRequest request, HttpServletResponse response) 4 throws ServletException, IOException { 5 6 request.setCharacterEncoding("utf-8"); 7 8 String id_1 = request.getParameter("id_1"); 9 String id = request.getParameter("id"); 10 String name = request.getParameter("name"); 11 String age = request.getParameter("age"); 12 String gender = request.getParameter("gender"); 13 String phone = request.getParameter("phone"); 14 String qq = request.getParameter("qq"); 15 String mail = request.getParameter("mail"); 16 17 try { 18 update(id_1,id,name,age,gender,phone,qq,mail,response); 19 } catch (Exception e) { 20 e.printStackTrace(); 21 } 22 } 23 24 public void update(String id_1, String id, String name, String age, 25 String gender, String phone, String qq, String mail, 26 HttpServletResponse response) throws Exception { 27 28 response.setContentType("text/html;charset=utf-8"); 29 //关联或者创建一个xml文档 30 // File file = new File("h:/testdemo/jiuye/myPhoneBook/Person.xml"); 31 File file = new File("/Person.xml"); 32 //判断xml文档是否存在 33 Document doc = new SAXReader().read(file); 34 Element rootElem = doc.getRootElement(); 35 36 37 Iterator<Element> perIt = rootElem.elementIterator("person"); 38 while (perIt.hasNext()) { 39 Element idEle = perIt.next().element("id"); 40 if(id_1.equals(idEle.getText())){ 41 //得到对应ID的父节点 42 Element parent = idEle.getParent(); 43 //根据父节点修改子节点的文本 44 parent.element("id").setText(id); 45 parent.element("name").setText(name); 46 parent.element("age").setText(age); 47 parent.element("gender").setText(gender); 48 parent.element("phone").setText(phone); 49 parent.element("qq").setText(qq); 50 parent.element("mail").setText(mail); 51 response.getWriter().write("<font color=\"red\" size=\"6px\">修改成功</font><a href=\"index.html\">返回首页</a>"); 52 save(file, doc); 53 } 54 } 55 } 56 57 private void save(File file, Document doc) throws FileNotFoundException, 58 UnsupportedEncodingException, IOException { 59 //将Document对象写入xml文档 60 FileOutputStream out = new FileOutputStream(file); 61 OutputFormat format = OutputFormat.createPrettyPrint(); 62 format.setEncoding("utf-8"); 63 XMLWriter writer = new XMLWriter(out,format); 64 writer.write(doc); 65 writer.close(); 66 } 67 68 }
3.删除联系人:
3.1删除逻辑:
1 public class Delete extends HttpServlet { 2 3 public void doPost(HttpServletRequest request, HttpServletResponse response) 4 throws ServletException, IOException { 5 //创建一个删除页面 6 try { 7 show(response); 8 } catch (Exception e) { 9 e.printStackTrace(); 10 } 11 12 request.setCharacterEncoding("utf-8"); 13 //获取要删除的联系人的ID 14 String temp = request.getParameter("choose"); 15 if(temp!=null){ 16 int choose = Integer.parseInt(temp); 17 try { 18 //进行删除操作 19 remove(choose,response); 20 } catch (Exception e) { 21 e.printStackTrace(); 22 } 23 } 24 } 25 protected void doGet(HttpServletRequest request, HttpServletResponse response) 26 throws ServletException, IOException { 27 this.doPost(request, response); 28 } 29 30 public void remove(int choose, HttpServletResponse response) throws Exception { 31 32 //关联或者创建一个xml文档 33 // File file = new File("h:/testdemo/jiuye/myPhoneBook/Person.xml"); 34 File file = new File("/Person.xml"); 35 Document doc = new SAXReader().read(file); 36 Element rootElem = doc.getRootElement(); 37 38 Element perElem = (Element) rootElem.elements().get(choose-1); 39 perElem.detach(); 40 save(file,doc); 41 response.getWriter().write("<center><font color=\"red\" size=\"6px\">删除成功</font><a href=\"index.html\">返回首页</a></center>"); 42 } 43 44 public void save(File file, Document doc) throws Exception { 45 //将Document对象写入xml文档 46 FileOutputStream out = new FileOutputStream(file); 47 OutputFormat format = OutputFormat.createPrettyPrint(); 48 format.setEncoding("utf-8"); 49 XMLWriter writer = new XMLWriter(out,format); 50 writer.write(doc); 51 writer.close(); 52 } 53 54 public void show(HttpServletResponse response) throws Exception { 55 56 response.setContentType("text/html;charset=utf-8"); 57 58 // File file = new File("h:/testdemo/jiuye/myPhoneBook/Person.xml"); 59 File file = new File("/Person.xml"); 60 response.getWriter().write("<html><head><title>联系人页面</title><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"></head><body><center><h1>联系人列表</h1></center><form action=\"/myPhoneBook/Delete\" method=\"POST\"><table cellpadding=\"10\" border=\"1\" align=\"center\"><tr align=\"center\"><td></td><td>编号</td><td>姓名</td><td>年龄</td><td>性别</td><td>电话</td><td>QQ</td><td>邮箱</td></tr>"); 61 Document doc = new SAXReader().read(file); 62 Element rootElem = doc.getRootElement(); 63 64 Iterator<Element> it = rootElem.elementIterator("person"); 65 int count = 1; 66 while(it.hasNext()){ 67 List<Element> list = it.next().elements(); 68 response.getWriter().write("<tr align=\"center\"><td>"+count+".<input type=\"radio\" name=\"choose\" value=\""+count+"\"/></td>"); 69 for (Element elem : list) { 70 response.getWriter().write("<td>"+elem.getText()+"</td>"); 71 } 72 response.getWriter().write("</tr>"); 73 count++; 74 } 75 response.getWriter().write("<tr><td colspan=\"8\" align=\"center\"><input type=\"submit\" value=\"删除联系人\"/><a href=\"index.html\">回到首页</a></td></tr></table></form>"); 76 } 77 78 }
4.显示联系人列表
public class PhoneList extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { showList(response); } catch (Exception e) { e.printStackTrace(); } } public void showList(HttpServletResponse response) throws Exception { response.setContentType("text/html;charset=utf-8"); // File file = new File("h:/testdemo/jiuye/myPhoneBook/Person.xml"); File file = new File("/Person.xml"); response.getWriter().write("<html><head><title>联系人页面</title><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"></head><body><center><h1>联系人列表</h1></center><table cellpadding=\"10\" border=\"1\" align=\"center\"><tr align=\"center\"><td>编号</td><td>姓名</td><td>年龄</td><td>性别</td><td>电话</td><td>QQ</td><td>邮箱</td></tr>"); if(!(file.exists())){ Document doc = DocumentHelper.createDocument(); Element rootElem = doc.addElement("Persons"); save(file,doc); } Document doc = new SAXReader().read(file); Element rootElem = doc.getRootElement(); Iterator<Element> it = rootElem.elementIterator("person"); while(it.hasNext()){ List<Element> list = it.next().elements(); response.getWriter().write("<tr align=\"center\">"); for (Element elem : list) { response.getWriter().write("<td>"+elem.getText()+"</td>"); } response.getWriter().write("</tr>"); } response.getWriter().write("</table><center><a href=\"index.html\">回到首页</a> <a href=\"add.html\">添加联系人</a> <a href=\"/myPhoneBook/Delete\">删除联系人</a></center>"); } public void save(File file, Document doc) throws IOException { //将Document对象写入xml文档 FileOutputStream out = new FileOutputStream(file); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter writer = new XMLWriter(out,format); writer.write(doc); writer.close(); } }
版权声明:本文为liangshandada原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。