一、通讯录项目

通讯录项目

通讯录源码下载。密码:86dd

	
	+++ 本项目展示项目的演变过程:
		一、通讯录项目版本 一 servlet+xml 版本 (基础版本)
		二、通讯录项目版本 一 servlet+jsp+xml 版本 (升级版本1.0)
		三、通讯录项目版本 一 servlet+jsp+mysql 版本 (升级版本 1.1)
 
 

一、通讯录项目 一 servlet+xml版本

1.1 注意事项:
	用xml作为数据库
	servlet写出动态页面来进行页面展示
1.2 项目位置

servlet+xml项目位置,密码:tun4

1.3 代码实现
ContactEntity.java

/**
 * 联系人实体
 * @author BGS
 *
 */
public class ContactEntity {
	
	private String id;
	private String name;
	private String address;
	private String phone;
	private String qq;
	private int age;
	private String sex;
	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 getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getQq() {
		return qq;
	}
	public void setQq(String qq) {
		this.qq = qq;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	
	@Override
	public String toString() {
		return "ContactEntity [id=" + id + ", name=" + name + ", address=" + address + ", phone=" + phone + ", qq=" + qq
				+ ", age=" + age + ", sex=" + sex + "]";
	}
	
	
	
	
}

XmlUtil.java
public class XmlUtil {
	
	
	/**
	 * 读取文档
	 * 
	 * @return
	 */
	public static Document readDoc() {
		
		initXml();
		Document doc=null;
		
		try {
			String file = XmlUtil.class.getResource("/contact.xml").getPath();
			System.out.println(file);
		    SAXReader reader = new SAXReader();
		    try {
				doc = reader.read(new FileReader(file));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    
		    
		} catch (DocumentException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} 
		
		return doc;
		
	}
	
	/**
	 *  初始化xml
	 */
	public static  void initXml() {
		
		//如果没有数据库就创建
		String file = XmlUtil.class.getResource("/").getPath()+"contact.xml";
		
		System.out.println(file);
		File f=new File(file);
		if(!f.exists()) {
			Document doc = DocumentHelper.createDocument();
			doc.setName("ContactList");
				XmlUtil.writeXml(doc);
			}
		
	}
	
	
	/**
	 * 写出xml配置文件
	 * 
	 * @param doc
	 */
	public static void writeXml(Document doc) {
		
		
		String file = XmlUtil.class.getResource("/").getPath()+"contact.xml";

		//设置格式
		OutputFormat format = OutputFormat.createCompactFormat();//设置紧凑的格式,去除空格和换行
		
		//写出xml文档
		try {
			XMLWriter xmlWriter=new XMLWriter(new FileWriter(file),format);
			xmlWriter.write(doc);
			
			xmlWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	}
}

ContactDao.java
public class ContactDao {
	
	/**
	 * 新增通讯人员
	 * 
	 * @param entity
	 */
	public void  addContact(ContactEntity  entity) {
		
	
		//获取跟标签
		Document doc = XmlUtil.readDoc();
		Element root = doc.getRootElement();
		
		
		//新增元素
		Element contact = root.addElement("contact");
		contact.addAttribute("id", UUID.randomUUID().toString());
		contact.addElement("name").setText( entity.getName());
		contact.addElement("address").setText( entity.getAddress());
		contact.addElement("phone").setText( entity.getPhone());
		contact.addElement("age").setText( entity.getAge()+"");
		contact.addElement("sex").setText( entity.getSex());
		contact.addElement("qq").setText( entity.getQq());

		//
		XmlUtil.writeXml(doc);
	}
	
	/**
	 * 修改通讯人员
	 * 
	 * @param entity
	 */
	public void  uptContact(ContactEntity  entity) {
	
		String id = entity.getId();
		
		//获取跟标签
		Document doc = XmlUtil.readDoc();
		
		//获取到目标标签
		Element contact = (Element) doc.selectSingleNode(String.format("//contact[@id='%s']",id));		
		
		//重新修改元素
		contact.element("name").setText(entity.getName());;
		contact.element("address").setText( entity.getAddress());
		contact.element("phone").setText(entity.getPhone());
		contact.element("age").setText( entity.getAge()+"");
		contact.element("sex").setText( entity.getSex());
		
		//
		XmlUtil.writeXml(doc);
	}
	
	
	/**
	 * 根据id查询
	 * 
	 * @param uid
	 * @return
	 */
	public ContactEntity  findById(String uid){
		
		Document doc = XmlUtil.readDoc();
		
		Element elem=(Element) doc.selectSingleNode(String.format("//contact[@id='%s']",uid));
		
		if(elem==null) {return null;}
		
		ContactEntity en=new ContactEntity();
		en.setId(elem.attribute("id").getValue());
		en.setName(elem.elementText("name"));
		en.setPhone(elem.elementText("phone"));
		en.setAddress(elem.elementText("address"));
		en.setAge(Integer.valueOf(elem.elementText("age")));
		en.setQq(elem.elementText("qq"));
		en.setSex(elem.elementText("sex"));
		
		return en;
		
	}

	
	/**
	 * 查询通讯录
	 * 
	 * @return
	 */
	public List<ContactEntity>  selectAll(){
		
		
		
		
		//获取doc文档。
		Document doc = XmlUtil.readDoc();
		
		//封装结果集
		List<ContactEntity>  result=new ArrayList<ContactEntity>();
		
		Element root = doc.getRootElement();
		
		List<Element> elements = root.elements();
		for(Element elem:elements) {
					
			
			ContactEntity en=new ContactEntity();
			en.setId(elem.attribute("id").getValue());
			en.setName(elem.elementText("name"));
			en.setPhone(elem.elementText("phone"));
			en.setAddress(elem.elementText("address"));
			en.setAge(Integer.valueOf(elem.elementText("age")));
			en.setQq(elem.elementText("qq"));
			en.setSex(elem.elementText("sex"));
			
			result.add(en);
		}
		
		return result;
		
	}
	
	
	/**
	 * 删除联系人
	 *  
	 * @return
	 */
	public void  delete(String id ){
		
		//获取doc文档。
		Document doc = XmlUtil.readDoc();
		
		String sel=String.format("//contact[@id='%s']",id);
		System.out.println(sel);
		
		
		Node node = doc.selectSingleNode(sel);
		
		node.detach();
		
		XmlUtil.writeXml(doc);
	}
	
	
	
	
	
}

SelectAllContactController.java
/**
 * 获取联系人列表
 * 
 * @author BGS
 *
 */
public class SelectAllContactController extends HttpServlet{
	
	
	private ContactDao dao=new ContactDao();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		List<ContactEntity> list = dao.selectAll();
		
		if(list==null) {
			list=new ArrayList<ContactEntity>();
		}
		
			
		String html="";
		
		html+="<!DOCTYPE html>                                                                                            ";
		html+="<html>                                                                                                     ";
		html+="<head>                                                                                                     ";
		html+="<meta charset=\"UTF-8\">                                                                                     ";
		html+="<title>Insert title here</title>                                                                           ";
		html+="</head>                                                                                                    ";
		html+="<body>                                                                                                     ";
		html+="	<table align=\"center\" border=\"1\" style=\"width:500px; margin-top:50px; text-align:center;font-size:17px; \">";
		html+="	                                                                                                          ";
		html+="		<caption sytle=\"align:center\">通讯录</caption>                                                        ";
		html+="		<tr>                                                                                                  ";
		html+="			<th>编号</th>                                                                                     ";
		html+="			<th>姓名</th>                                                                                     ";
		html+="			<th>性别</th>                                                                                     ";
		html+="			<th>年龄</th>                                                                                     ";
		html+="                                                                                                           ";
		html+="			<th>手机</th>                                                                                     ";
		html+="			<th>QQ</th>                                                                                       ";
		html+="			<th>地址</th>                                                                                     ";
		html+="			<th></th>                                                                                         ";
		html+="                                                                                                           ";
		html+="		</tr>                                                                                                 ";
		
		int i=0;
		for(ContactEntity en:list) {
			html+="		<tr>  ";
			html+="			<td>"+(i++)+"</td>                                                                                        ";
			html+="			<td>"+en.getName()+"</td>                                                                                        ";
			html+="			<td>"+en.getSex()+"</td>                                                                                        ";
			html+="			<td>"+en.getAge()+"</td>                                                                                        ";
			html+="			<td>"+en.getPhone()+"</td>                                                                                        ";
			html+="			<td>"+en.getQq()+"</td>                                                                                        ";
			html+="			<td>"+en.getAddress()+"</td>                                                                                        ";
			html+="			<td><a  href=\"./update?id="+en.getId()+"\">修改</a>&nbsp;<a  href=\"./delete?id="+en.getId()+"\">删除</a></td>                                         ";
			html+="		</tr>  ";
		}
		
		html+="		<tr >                                                                                                 ";
		html+="			<td colspan=\"8\" ><a href=\"./add\">添加联系人</a></td>                                                  ";
		html+="		</tr>                                                                                                 ";
		html+="	                                                                                                          ";
		html+="	</table>                                                                                                  ";
		html+="</body>                                                                                                    ";
		html+="</html>                                                                                                    ";
		
		
		resp.getWriter().write(html);

	}
}

InsertContactController.java
/**
 * 新增联系人
 * 
 * @author BGS
 *
 */
public class InsertContactController extends HttpServlet {
	
	private ContactDao dao=new ContactDao();

	/**
	 * 输出新增联系人表单
	 * 
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		
		String html="";
		html+="<!DOCTYPE html>                                                                                                                                                                     ";
		html+="<html>                                                                                                                                                                              ";
		html+="<head>                                                                                                                                                                              ";
		html+="<meta charset=\"UTF-8\">                                                                                                                                                              ";
		html+="<title>Insert title here</title>                                                                                                                                                    ";
		html+="</head>                                                                                                                                                                             ";
		html+="<body>                                                                                                                                                                              ";
		html+="<body>             ";
		
		html+="<form action=\"./add\" method=\"post\">";

		
		html+="	<table align=\"center\" border=\"1\" style=\"width:500px; margin-top:50px; text-align:center;font-size:17px;\">                                                                          ";
		html+="	                                                                                                                                                                                   ";
		html+="        <caption style=\"align:center\">新增联系人</caption>                                                                                                                          ";
		html+="		                                                                                                                                                                               ";
		html+="		<tr>                                                                                                                                                                           ";
		html+="			<th>姓名</th>                                                                                                                                                              ";
		html+="			<td  ><input type=\"text\" name=\"name\"/></td>                                                                                                                                ";
		html+="		</tr>                                                                                                                                                                          ";
		html+="		<tr>                                                                                                                                                                           ";
		html+="			<th>性别</th>                                                                                                                                                              ";
		html+="			<td><input type=\"text\" name=\"sex\"/></td>                                                                                                                                   ";
		html+="		</tr>                                                                                                                                                                          ";
		html+="		<tr>                                                                                                                                                                           ";
		html+="			<th>年龄</th>                                                                                                                                                              ";
		html+="			<td><input type=\"text\" name=\"age\"/></td>                                                                                                                                   ";
		html+="		</tr>                                                                                                                                                                          ";
		html+="		<tr>                                                                                                                                                                           ";
		html+="			<th>手机</th>                                                                                                                                                              ";
		html+="			<td><input type=\"text\" name=\"phone\"/></td>                                                                                                                                 ";
		html+="		</tr>                                                                                                                                                                          ";
		html+="		<tr>                                                                                                                                                                           ";
		html+="			<th>QQ</th>                                                                                                                                                                ";
		html+="			<td><input type=\"text\" name=\"qq\"/></td>                                                                                                                                    ";
		html+="		</tr>                                                                                                                                                                          ";
		html+="		<tr>                                                                                                                                                                           ";
		html+="			<th>地址</th>                                                                                                                                                              ";
		html+="			<td><input type=\"text\" name=\"address\"/></td>                                                                                                                               ";
		html+="		</tr>                                                                                                                                                                          ";
		html+="		 		                                                                                                                                                                       ";
		html+="	                                                                                                                                                                                   ";
		html+="		<tr >                                                                                                                                                                          ";
		html+="				<td colspan=\"2\" ><input type=\"submit\"value=\"新增\" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"./list\">返回</a></td> ";
		html+="		</tr>                                                                                                                                                                          ";
		html+="	                                                                                                                                                                                   ";
		html+="	</table>                                                                                                                                                                           ";
		
		html+="</form >";

		html+="</body>                                                                                                                                                                             ";
		html+="</html>                                                                                                                                                                             ";
		
		resp.getWriter().write(html);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");		
		resp.setContentType("text/html;charset=utf-8");
	
		String name = req.getParameter("name");
		String address = req.getParameter("address");
		String phone = req.getParameter("phone");
		String qq = req.getParameter("qq");
		String age = req.getParameter("age");
		String sex = req.getParameter("sex");
		
		
		ContactEntity en=new ContactEntity();
		en.setName(name);
		en.setAddress(address);
		en.setPhone(phone);
		en.setQq(qq);
		en.setAge(age==null||age==""?0:Integer.valueOf(age));
		en.setSex(sex);
		
		dao.addContact(en);
		
		
		
		resp.getWriter().write("新增成功  <a href=\"./list\">返回</a>");
		
		resp.setHeader("refresh", "3;url=./list"); // 隔3s后跳转到其他资源

	}
}

UpdateContactController.java
public class UpdateContactController extends HttpServlet {
	
	private ContactDao dao=new ContactDao();

	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		String id = req.getParameter("id");
		
		ContactEntity en = dao.findById(id);
		
		String html="";
		html+="<!DOCTYPE html>                                                                                                                                                                     ";
		html+="<html>                                                                                                                                                                              ";
		html+="<head>                                                                                                                                                                              ";
		html+="<meta charset=\"UTF-8\">                                                                                                                                                              ";
		html+="<title>Insert title here</title>                                                                                                                                                    ";
		html+="</head>                                                                                                                                                                             ";
		html+="<body>                                                                                                                                                                              ";
		html+="<body>             ";
		
		html+="<form action=\"./update\" method=\"post\">";

		html+="	<input type=\"text\" name=\"id\" hidden=\"true\" value='"+en.getId()+"'/>                                                                                                                               ";

		
		html+="	<table align=\"center\" border=\"1\" style=\"width:500px; margin-top:50px; text-align:center;font-size:17px;\">                                                                          ";
		html+="	                                                                                                                                                                                   ";
		html+="        <caption style=\"align:center\">修改联系人</caption>                                                                                                                          ";
		html+="		                                                                                                                                                                               ";
		html+="		<tr>                                                                                                                                                                           ";
		html+="			<th>姓名</th>                                                                                                                                                              ";
		html+="			<td  ><input type=\"text\" name=\"name\" value='"+en.getName()+"'/></td>                                                                                                                                ";
		html+="		</tr>                                                                                                                                                                          ";
		html+="		<tr>                                                                                                                                                                           ";
		html+="			<th>性别</th>                                                                                                                                                              ";
		html+="			<td><input type=\"text\" name=\"sex\"  value='"+en.getSex()+"'/></td>                                                                                                                                   ";
		html+="		</tr>                                                                                                                                                                          ";
		html+="		<tr>                                                                                                                                                                           ";
		html+="			<th>年龄</th>                                                                                                                                                              ";
		html+="			<td><input type=\"text\" name=\"age\"  value='"+en.getAge()+"'/></td>                                                                                                                                   ";
		html+="		</tr>                                                                                                                                                                          ";
		html+="		<tr>                                                                                                                                                                           ";
		html+="			<th>手机</th>                                                                                                                                                              ";
		html+="			<td><input type=\"text\" name=\"phone\"  value='"+en.getPhone()+"'/></td>                                                                                                                                 ";
		html+="		</tr>                                                                                                                                                                          ";
		html+="		<tr>                                                                                                                                                                           ";
		html+="			<th>QQ</th>                                                                                                                                                                ";
		html+="			<td><input type=\"text\" name=\"qq\"  value='"+en.getQq()+"'/></td>                                                                                                                                    ";
		html+="		</tr>                                                                                                                                                                          ";
		html+="		<tr>                                                                                                                                                                           ";
		html+="			<th>地址</th>                                                                                                                                                              ";
		html+="			<td><input type=\"text\" name=\"address\"  value='"+en.getAddress()+"'/></td>                                                                                                                               ";
		html+="		</tr>                                                                                                                                                                          ";
		html+="		 		                                                                                                                                                                       ";
		html+="	                                                                                                                                                                                   ";
		html+="		<tr >                                                                                                                                                                          ";
		html+="				<td colspan=\"2\" ><input type=\"submit\"value=\"修改\" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"./list\">返回</a></td> ";
		html+="		</tr>                                                                                                                                                                          ";
		html+="	                                                                                                                                                                                   ";
		html+="	</table>                                                                                                                                                                           ";
		
		html+="</form >";

		html+="</body>                                                                                                                                                                             ";
		html+="</html>                                                                                                                                                                             ";
		
		resp.getWriter().write(html);

	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			
		
		req.setCharacterEncoding("utf-8");		
		resp.setContentType("text/html;charset=utf-8");
				
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String address = req.getParameter("address");
		String phone = req.getParameter("phone");
		String qq = req.getParameter("qq");
		String age = req.getParameter("age");
		String sex = req.getParameter("sex");
		
		
		ContactEntity en=new ContactEntity();
		en.setId(id);
		en.setName(name);
		en.setAddress(address);
		en.setPhone(phone);
		en.setQq(qq);
		en.setAge(age==null||age==""?0:Integer.valueOf(age));
		en.setSex(sex);
		
		dao.uptContact(en);
		
		resp.getWriter().write("修改成功  <a href=\"./list\">返回</a>");


	}
}

DeleteContactController.java
public class DeleteContactController extends HttpServlet {
	
	private ContactDao dao=new ContactDao();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");		
		resp.setContentType("text/html;charset=utf-8");
	
		
		String id = req.getParameter("id");
		
		dao.delete(id);
		
		resp.getWriter().write("删除成功  <a href=\"./list\">返回</a>");

	}
}

展示结果

list页面
在这里插入图片描述

add页面
在这里插入图片描述

update页面
在这里插入图片描述

二、通讯录项目 一 servlet+jsp+xml版本

1.1 注意事项:
	用xml作为数据库
	用jsp作为页面展示
	用servlet写出处理业务逻辑
1.2 项目位置

servlet+xml项目位置,密码:tun4

1.3 代码实现
ContactEntity.java

/**
 * 联系人实体
 * @author BGS
 *
 */
public class ContactEntity {
	
	private String id;
	private String name;
	private String address;
	private String phone;
	private String qq;
	private int age;
	private String sex;
	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 getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getQq() {
		return qq;
	}
	public void setQq(String qq) {
		this.qq = qq;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	
	@Override
	public String toString() {
		return "ContactEntity [id=" + id + ", name=" + name + ", address=" + address + ", phone=" + phone + ", qq=" + qq
				+ ", age=" + age + ", sex=" + sex + "]";
	}
	
	
	
	
}

XmlUtil.java
public class XmlUtil {
	
	
	/**
	 * 读取文档
	 * 
	 * @return
	 */
	public static Document readDoc() {
		
		initXml();
		Document doc=null;
		
		try {
			String file = XmlUtil.class.getResource("/contact.xml").getPath();
			System.out.println(file);
		    SAXReader reader = new SAXReader();
		    try {
				doc = reader.read(new FileReader(file));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    
		    
		} catch (DocumentException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} 
		
		return doc;
		
	}
	
	/**
	 *  初始化xml
	 */
	public static  void initXml() {
		
		//如果没有数据库就创建
		String file = XmlUtil.class.getResource("/").getPath()+"contact.xml";
		
		System.out.println(file);
		File f=new File(file);
		if(!f.exists()) {
			Document doc = DocumentHelper.createDocument();
			doc.setName("ContactList");
				XmlUtil.writeXml(doc);
			}
		
	}
	
	
	/**
	 * 写出xml配置文件
	 * 
	 * @param doc
	 */
	public static void writeXml(Document doc) {
		
		
		String file = XmlUtil.class.getResource("/").getPath()+"contact.xml";

		//设置格式
		OutputFormat format = OutputFormat.createCompactFormat();//设置紧凑的格式,去除空格和换行
		
		//写出xml文档
		try {
			XMLWriter xmlWriter=new XMLWriter(new FileWriter(file),format);
			xmlWriter.write(doc);
			
			xmlWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	}
}

ContactDao.java
public class ContactDao {
	
	/**
	 * 新增通讯人员
	 * 
	 * @param entity
	 */
	public void  addContact(ContactEntity  entity) {
		
	
		//获取跟标签
		Document doc = XmlUtil.readDoc();
		Element root = doc.getRootElement();
		
		
		//新增元素
		Element contact = root.addElement("contact");
		contact.addAttribute("id", UUID.randomUUID().toString());
		contact.addElement("name").setText( entity.getName());
		contact.addElement("address").setText( entity.getAddress());
		contact.addElement("phone").setText( entity.getPhone());
		contact.addElement("age").setText( entity.getAge()+"");
		contact.addElement("sex").setText( entity.getSex());
		contact.addElement("qq").setText( entity.getQq());

		//
		XmlUtil.writeXml(doc);
	}
	
	/**
	 * 修改通讯人员
	 * 
	 * @param entity
	 */
	public void  uptContact(ContactEntity  entity) {
	
		String id = entity.getId();
		
		//获取跟标签
		Document doc = XmlUtil.readDoc();
		
		//获取到目标标签
		Element contact = (Element) doc.selectSingleNode(String.format("//contact[@id='%s']",id));		
		
		//重新修改元素
		contact.element("name").setText(entity.getName());;
		contact.element("address").setText( entity.getAddress());
		contact.element("phone").setText(entity.getPhone());
		contact.element("age").setText( entity.getAge()+"");
		contact.element("sex").setText( entity.getSex());
		
		//
		XmlUtil.writeXml(doc);
	}
	
	
	/**
	 * 根据id查询
	 * 
	 * @param uid
	 * @return
	 */
	public ContactEntity  findById(String uid){
		
		Document doc = XmlUtil.readDoc();
		
		Element elem=(Element) doc.selectSingleNode(String.format("//contact[@id='%s']",uid));
		
		if(elem==null) {return null;}
		
		ContactEntity en=new ContactEntity();
		en.setId(elem.attribute("id").getValue());
		en.setName(elem.elementText("name"));
		en.setPhone(elem.elementText("phone"));
		en.setAddress(elem.elementText("address"));
		en.setAge(Integer.valueOf(elem.elementText("age")));
		en.setQq(elem.elementText("qq"));
		en.setSex(elem.elementText("sex"));
		
		return en;
		
	}

	
	/**
	 * 查询通讯录
	 * 
	 * @return
	 */
	public List<ContactEntity>  selectAll(){
		
		
		
		
		//获取doc文档。
		Document doc = XmlUtil.readDoc();
		
		//封装结果集
		List<ContactEntity>  result=new ArrayList<ContactEntity>();
		
		Element root = doc.getRootElement();
		
		List<Element> elements = root.elements();
		for(Element elem:elements) {
					
			
			ContactEntity en=new ContactEntity();
			en.setId(elem.attribute("id").getValue());
			en.setName(elem.elementText("name"));
			en.setPhone(elem.elementText("phone"));
			en.setAddress(elem.elementText("address"));
			en.setAge(Integer.valueOf(elem.elementText("age")));
			en.setQq(elem.elementText("qq"));
			en.setSex(elem.elementText("sex"));
			
			result.add(en);
		}
		
		return result;
		
	}
	
	
	/**
	 * 删除联系人
	 *  
	 * @return
	 */
	public void  delete(String id ){
		
		//获取doc文档。
		Document doc = XmlUtil.readDoc();
		
		String sel=String.format("//contact[@id='%s']",id);
		System.out.println(sel);
		
		
		Node node = doc.selectSingleNode(sel);
		
		node.detach();
		
		XmlUtil.writeXml(doc);
	}
	
	
	
	
	
}

SelectAllContactController.java
/**
 * 获取联系人列表
 * 
 * @author BGS
 *
 */
public class SelectAllContactController extends HttpServlet{
	
	
	private ContactDao dao=new ContactDao();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		
		List<ContactEntity> list = dao.selectAll();
		
		req.setAttribute("list", list);
		
		req.getRequestDispatcher("./list.jsp").forward(req, resp);;

	}
}


InsertContactController.java
/**
 * 新增联系人
 * 
 * @author BGS
 *
 */
public class InsertContactController extends HttpServlet {
	
	private ContactDao dao=new ContactDao();

	

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");		
		resp.setContentType("text/html;charset=utf-8");
	
		String name = req.getParameter("name");
		String address = req.getParameter("address");
		String phone = req.getParameter("phone");
		String qq = req.getParameter("qq");
		String age = req.getParameter("age");
		String sex = req.getParameter("sex");
		
		
		ContactEntity en=new ContactEntity();
		en.setName(name);
		en.setAddress(address);
		en.setPhone(phone);
		en.setQq(qq);
		en.setAge(age==null||age==""?0:Integer.valueOf(age));
		en.setSex(sex);
		
		dao.addContact(en);
		
		
		
		resp.getWriter().write("新增成功  <a href=\"./list\">返回</a>");
		
		resp.setHeader("refresh", "3;url=./list"); // 隔3s后跳转到其他资源

	}
}


UpdateContactController.java
public class UpdateContactController extends HttpServlet {
	
	private ContactDao dao=new ContactDao();

	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		String id = req.getParameter("id");
		
		ContactEntity en = dao.findById(id);
		
		req.setAttribute("item", en);
		
		req.getRequestDispatcher("./update.jsp").forward(req, resp);
		
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			
		
		req.setCharacterEncoding("utf-8");		
		resp.setContentType("text/html;charset=utf-8");
				
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String address = req.getParameter("address");
		String phone = req.getParameter("phone");
		String qq = req.getParameter("qq");
		String age = req.getParameter("age");
		String sex = req.getParameter("sex");
		
		
		ContactEntity en=new ContactEntity();
		en.setId(id);
		en.setName(name);
		en.setAddress(address);
		en.setPhone(phone);
		en.setQq(qq);
		en.setAge(age==null||age==""?0:Integer.valueOf(age));
		en.setSex(sex);
		
		dao.uptContact(en);
		
		resp.getWriter().write("修改成功  <a href=\"./list\">返回</a>");


	}
}


DeleteContactController.java
public class DeleteContactController extends HttpServlet {
	
	private ContactDao dao=new ContactDao();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");		
		resp.setContentType("text/html;charset=utf-8");
	
		
		String id = req.getParameter("id");
		
		dao.delete(id);
		
		resp.getWriter().write("删除成功  <a href=\"./list\">返回</a>");

	}
}

list.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="UTF-8"%>
    
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>   
 
 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>联系人列表</title>
</head>
<body>
	<table align="center" border="1" style="width:500px; margin-top:50px; text-align:center;font-size:17px;">
	                                                                                                        
		<caption sytle="align:center">通讯录</caption>                                                      
		<tr>                                                                                                
			<th>编号</th>                                                                                   
			<th>姓名</th>                                                                                   
			<th>性别</th>                                                                                   
			<th>年龄</th>                                                                                   
                                                                                                           
			<th>手机</th>                                                                                   
			<th>QQ</th>                                                                                     
			<th>地址</th>
			<th>     </th>                                                                                     
                                                                                                    
	</tr> 
	
	    <c:forEach items="${list}"  var="item" varStatus="indexObj">
	    	<tr>                                                                                            
				<td>${indexObj.index}</td>                                                                                  
				<td>${item.name}</td>                                                                                  
				<td>${item.sex}</td>                                                                                  
				<td>${item.age}</td>                                                                                  
		                                                                                                    
				<td>${item.phone}</td>                                                                                  
				<td>${item.qq}</td>                                                                                  
				<td>${item.address}</td>                                                                                  
                                                                       
				<td><a  href="./update?id=${item.id}">修改</a>&nbsp;<a  href="./delete?id=${item.id}">删除</a></td>                                   
			</tr>                                                                                           
	    </c:forEach>                                                                                 
	
	<tr >                                                                                           
		<td colspan="8" ><a href="./add.jsp">添加联系人</a></td>                                            
	</tr>                                                                                           
                                                                                                    
	</table>  
</body>
</html>
add.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	
	<form action="./add" method="post">
	
	<table align="center" border="1" style="width:500px; margin-top:50px; text-align:center;font-size:17px;">
	                                                                                                       
        <caption style="align:center">新增联系人</caption>
		
		<tr>                                                                                                                                                                           
			<th>姓名</th>  
			<td  ><input type="text" name="name"/></td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>性别</th>                                                                                  
			<td>
				<input   type="radio" name="sex" value="男"/>男
				<input type="radio" name="sex" value="女"/>女
			</td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>年龄</th>                                                                                  
			<td><input type="text" name="age"/></td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>手机</th>                                                                                  
			<td><input type="text" name="phone"/></td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>地址</th>                                                                                  
			<td><input type="text" name="address"/></td>                                                                                
		</tr>  
		 <tr>                                                                                                                                                                           
			<th>QQ</th>                                                                                  
			<td><input type="text" name="qq"/></td>                                                                                
		</tr>  		                                                   
	                                                                                                                                                                            
		<tr >                                                                                              
			<td colspan="2" ><input type="submit"value="新增" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#">返回</a></td>                                               
		</tr>                                                                                      
	                                                                                                       
	</table>
	</form>                                                                                                 
	
</body>
</html>
update.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
 
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>   

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>修改联系人</title>
</head>
<body>
	
	<form action="./update" method="post">

	<input type="text" name="id" hidden="true" value="${item.id}"/>                                                                                
	
	<table align="center" border="1" style="width:500px; margin-top:50px; text-align:center;font-size:17px;">
	                                                                                                       
        <caption style="align:center">修改联系人</caption>
		
		<tr>                                                                                                                                                                           
			<th>姓名</th>  
			<td  ><input type="text" name="name" value="${item.name}"/></td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>性别</th>                                                                                  
			<td>
				<c:if  test="${item.sex =='男'}">
					<input   type="radio" value="男" checked="true" name="sex"/>男
					<input type="radio" value="女" name="sex"/>女
				</c:if>
				<c:if  test="${item.sex =='女' }">
					<input   type="radio" value="男" name="sex"/>男
					<input type="radio"  value="女" checked="true" name="sex"/>女
				</c:if>
			</td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>年龄</th>                                                                                  
			<td><input type="text" name="age"  value="${item.age}"/></td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>手机</th>                                                                                  
			<td><input type="text" name="phone"  value="${item.phone}"/></td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>地址</th>                                                                                  
			<td><input type="text" name="address"  value="${item.address}"/></td>                                                                                
		</tr>  
		 		                                                   
	                                                                                                                                                                            
		<tr >                                                                                              
			<td colspan="2" ><input type="submit"value="修改" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#">返回</a></td>                                               
		</tr>                                                                                      
	                                                                                                       
	</table>
	</form>                                                                                                 
	
</body>
</html>
展示结果

list页面
在这里插入图片描述

add页面
在这里插入图片描述

update页面
在这里插入图片描述

三、通讯录项目 一 servlet+jsp+mysql 版本

1.1 注意事项:
	用mysql作为数据库
	用jsp作为页面展示
	用servlet写出处理业务逻辑
1.2 代码实现

mysql脚本,创建数据库


	 CREATE TABLE contact(
			id VARCHAR(100),
			NAME VARCHAR(20),
			address VARCHAR(20),
			phone VARCHAR(20),
			qq VARCHAR(20),
			age INT,
			sex VARCHAR(20)
		)
		
ContactEntity.java

/**
 * 联系人实体
 * @author BGS
 *
 */
public class ContactEntity {
	
	private String id;
	private String name;
	private String address;
	private String phone;
	private String qq;
	private int age;
	private String sex;
	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 getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getQq() {
		return qq;
	}
	public void setQq(String qq) {
		this.qq = qq;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	
	@Override
	public String toString() {
		return "ContactEntity [id=" + id + ", name=" + name + ", address=" + address + ", phone=" + phone + ", qq=" + qq
				+ ", age=" + age + ", sex=" + sex + "]";
	}
	
	
	
	
}

JdbcUtil .java
public class JdbcUtil {
	
	private static String user="root";
	
	private static String password="root";
	
	private static String url="jdbc:mysql://localhost:3306/day11";
	
	static {
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	
	
	/**
	 * 获取连接
	 * 
	 * @return
	 */
	public static Connection getConn() {
		
		Connection conn =null;
		try {
			 conn = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		
		return conn;
		
	}
	
	/**
	 * 关闭资源
	 * 
	 * @param conn
	 * @param st
	 */
	public static void close(Connection conn,Statement st) {
		
		try {
	
			if(st!=null) {
				st.close();
			}
			if(conn!=null) {
				conn.close();
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	
	
	/**
	 * 关闭资源
	 * 
	 * @param conn
	 * @param st
	 * @param rs
	 */
	public static void close(Connection conn,Statement st,ResultSet rs) {
		
		try {
			
			if(rs!=null) {
				rs.close();
			}
			if(st!=null) {
				st.close();
			}
			if(conn!=null) {
				conn.close();
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	
}


ContactDao.java
public class ContactDao {
	
	/**
	 * 新增通讯人员
	 * 
	 * @param entity
	 */
	public void  addContact(ContactEntity  entity) {
		
		entity.setId(UUID.randomUUID().toString());

		
		Connection conn = JdbcUtil.getConn();
		PreparedStatement st=null;
		
		try {
			
			String sql="insert into contact values(?,?,?,?,?,?,?) ";

			conn = JdbcUtil.getConn();
			st = conn.prepareStatement(sql);
			
			st.setString(1, entity.getId());
			st.setString(2, entity.getName());
			st.setString(3, entity.getAddress());
			st.setString(4, entity.getPhone());
			st.setString(5, entity.getQq());
			st.setInt(6, entity.getAge());
			st.setString(7, entity.getSex());
			
			int row = st.executeUpdate();
			System.out.println("新增一条数据");
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.close(conn, st);
		}

	}
	
	/**
	 * 修改通讯人员
	 * 
	 * @param entity
	 */
	public void  uptContact(ContactEntity  entity) {
		
		Connection conn = JdbcUtil.getConn();
		PreparedStatement st=null;
		
		try {
			
			String sql="update contact set name=?,address=?,phone=?,qq=?,age=?,sex=? where id=? ";

			conn = JdbcUtil.getConn();
			st = conn.prepareStatement(sql);
			
			st.setString(1, entity.getName());
			st.setString(2, entity.getAddress());
			st.setString(3, entity.getPhone());
			st.setString(4, entity.getQq());
			st.setInt(5, entity.getAge());
			st.setString(6, entity.getSex());
			st.setString(7, entity.getId());
			
			int row = st.executeUpdate();
			System.out.println("修改一条数据");

		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.close(conn, st);
		}


	}
	
	
	/**
	 * 根据id查询
	 * 
	 * @param uid
	 * @return
	 */
	public ContactEntity  findById(String uid){
		

		Connection conn = JdbcUtil.getConn();
		PreparedStatement st=null;
		ContactEntity item =null;

		try {
			
			String sql="select * from contact where id=?";

			conn = JdbcUtil.getConn();
			st = conn.prepareStatement(sql);
	
			st.setString(1, uid);
			
			ResultSet rs = st.executeQuery();
			while(rs.next()) {
				
				item =new ContactEntity();
				
				item.setName(rs.getString("name"));
				item.setId(rs.getString("id"));
				item.setPhone(rs.getString("phone"));
				item.setQq(rs.getString("qq"));
				item.setSex(rs.getString("sex"));
				item.setAddress(rs.getString("address"));
				item.setAge(Integer.valueOf(rs.getString("age")));
				
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.close(conn, st);
		}
		
		return item;
	}

	
	/**
	 * 查询通讯录
	 * 
	 * @return
	 */
	public List<ContactEntity>  selectAll(){

		Connection conn = JdbcUtil.getConn();
		PreparedStatement st=null;
		List<ContactEntity> list =new ArrayList<ContactEntity>();

		try {
			
			String sql="select * from contact";

			conn = JdbcUtil.getConn();
			st = conn.prepareStatement(sql);
	
			
			ResultSet rs = st.executeQuery();
			while(rs.next()) {
				
				ContactEntity item =new ContactEntity();
				
				item.setName(rs.getString("name"));
				item.setId(rs.getString("id"));
				item.setPhone(rs.getString("phone"));
				item.setQq(rs.getString("qq"));
				item.setSex(rs.getString("sex"));
				item.setAddress(rs.getString("address"));
				item.setAge(Integer.valueOf(rs.getString("age")));
				
				list.add(item);
			}
			
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.close(conn, st);
		}
		
		return list;
		
	}
	
	
	/**
	 * 删除联系人
	 *  
	 * @return
	 */
	public void  delete(String id ){
		Connection conn = JdbcUtil.getConn();
		PreparedStatement st=null;
		
		try {
			
			String sql="delete from contact where id=? ";

			conn = JdbcUtil.getConn();
			st = conn.prepareStatement(sql);
			
			st.setString(1, id);
			
			int row = st.executeUpdate();
			System.out.println("删除了一条数据");

		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.close(conn, st);
		}


	}
	
	
	
	
	
}


SelectAllContactController.java
/**
 * 获取联系人列表
 * 
 * @author BGS
 *
 */
public class SelectAllContactController extends HttpServlet{
	
	
	private ContactDao dao=new ContactDao();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		
		List<ContactEntity> list = dao.selectAll();
		
		req.setAttribute("list", list);
		
		req.getRequestDispatcher("./list.jsp").forward(req, resp);;

	}
}


InsertContactController.java
/**
 * 新增联系人
 * 
 * @author BGS
 *
 */
public class InsertContactController extends HttpServlet {
	
	private ContactDao dao=new ContactDao();

	

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");		
		resp.setContentType("text/html;charset=utf-8");
	
		String name = req.getParameter("name");
		String address = req.getParameter("address");
		String phone = req.getParameter("phone");
		String qq = req.getParameter("qq");
		String age = req.getParameter("age");
		String sex = req.getParameter("sex");
		
		
		ContactEntity en=new ContactEntity();
		en.setName(name);
		en.setAddress(address);
		en.setPhone(phone);
		en.setQq(qq);
		en.setAge(age==null||age==""?0:Integer.valueOf(age));
		en.setSex(sex);
		
		dao.addContact(en);
		
		
		
		resp.getWriter().write("新增成功  <a href=\"./list\">返回</a>");
		
		resp.setHeader("refresh", "3;url=./list"); // 隔3s后跳转到其他资源

	}
}


UpdateContactController.java
public class UpdateContactController extends HttpServlet {
	
	private ContactDao dao=new ContactDao();

	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		String id = req.getParameter("id");
		
		ContactEntity en = dao.findById(id);
		
		req.setAttribute("item", en);
		
		req.getRequestDispatcher("./update.jsp").forward(req, resp);
		
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			
		
		req.setCharacterEncoding("utf-8");		
		resp.setContentType("text/html;charset=utf-8");
				
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String address = req.getParameter("address");
		String phone = req.getParameter("phone");
		String qq = req.getParameter("qq");
		String age = req.getParameter("age");
		String sex = req.getParameter("sex");
		
		
		ContactEntity en=new ContactEntity();
		en.setId(id);
		en.setName(name);
		en.setAddress(address);
		en.setPhone(phone);
		en.setQq(qq);
		en.setAge(age==null||age==""?0:Integer.valueOf(age));
		en.setSex(sex);
		
		dao.uptContact(en);
		
		resp.getWriter().write("修改成功  <a href=\"./list\">返回</a>");


	}
}


DeleteContactController.java
public class DeleteContactController extends HttpServlet {
	
	private ContactDao dao=new ContactDao();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");		
		resp.setContentType("text/html;charset=utf-8");
	
		
		String id = req.getParameter("id");
		
		dao.delete(id);
		
		resp.getWriter().write("删除成功  <a href=\"./list\">返回</a>");

	}
}

list.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="UTF-8"%>
    
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>   
 
 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>联系人列表</title>
</head>
<body>
	<table align="center" border="1" style="width:500px; margin-top:50px; text-align:center;font-size:17px;">
	                                                                                                        
		<caption sytle="align:center">通讯录</caption>                                                      
		<tr>                                                                                                
			<th>编号</th>                                                                                   
			<th>姓名</th>                                                                                   
			<th>性别</th>                                                                                   
			<th>年龄</th>                                                                                   
                                                                                                           
			<th>手机</th>                                                                                   
			<th>QQ</th>                                                                                     
			<th>地址</th>
			<th>     </th>                                                                                     
                                                                                                    
	</tr> 
	
	    <c:forEach items="${list}"  var="item" varStatus="indexObj">
	    	<tr>                                                                                            
				<td>${indexObj.index}</td>                                                                                  
				<td>${item.name}</td>                                                                                  
				<td>${item.sex}</td>                                                                                  
				<td>${item.age}</td>                                                                                  
		                                                                                                    
				<td>${item.phone}</td>                                                                                  
				<td>${item.qq}</td>                                                                                  
				<td>${item.address}</td>                                                                                  
                                                                       
				<td><a  href="./update?id=${item.id}">修改</a>&nbsp;<a  href="./delete?id=${item.id}">删除</a></td>                                   
			</tr>                                                                                           
	    </c:forEach>                                                                                 
	
	<tr >                                                                                           
		<td colspan="8" ><a href="./add.jsp">添加联系人</a></td>                                            
	</tr>                                                                                           
                                                                                                    
	</table>  
</body>
</html>
add.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	
	<form action="./add" method="post">
	
	<table align="center" border="1" style="width:500px; margin-top:50px; text-align:center;font-size:17px;">
	                                                                                                       
        <caption style="align:center">新增联系人</caption>
		
		<tr>                                                                                                                                                                           
			<th>姓名</th>  
			<td  ><input type="text" name="name"/></td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>性别</th>                                                                                  
			<td>
				<input   type="radio" name="sex" value="男"/>男
				<input type="radio" name="sex" value="女"/>女
			</td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>年龄</th>                                                                                  
			<td><input type="text" name="age"/></td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>手机</th>                                                                                  
			<td><input type="text" name="phone"/></td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>地址</th>                                                                                  
			<td><input type="text" name="address"/></td>                                                                                
		</tr>  
		 <tr>                                                                                                                                                                           
			<th>QQ</th>                                                                                  
			<td><input type="text" name="qq"/></td>                                                                                
		</tr>  		                                                   
	                                                                                                                                                                            
		<tr >                                                                                              
			<td colspan="2" ><input type="submit"value="新增" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#">返回</a></td>                                               
		</tr>                                                                                      
	                                                                                                       
	</table>
	</form>                                                                                                 
	
</body>
</html>
update.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
 
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>   

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>修改联系人</title>
</head>
<body>
	
	<form action="./update" method="post">

	<input type="text" name="id" hidden="true" value="${item.id}"/>                                                                                
	
	<table align="center" border="1" style="width:500px; margin-top:50px; text-align:center;font-size:17px;">
	                                                                                                       
        <caption style="align:center">修改联系人</caption>
		
		<tr>                                                                                                                                                                           
			<th>姓名</th>  
			<td  ><input type="text" name="name" value="${item.name}"/></td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>性别</th>                                                                                  
			<td>
				<c:if  test="${item.sex =='男'}">
					<input   type="radio" value="男" checked="true" name="sex"/>男
					<input type="radio" value="女" name="sex"/>女
				</c:if>
				<c:if  test="${item.sex =='女' }">
					<input   type="radio" value="男" name="sex"/>男
					<input type="radio"  value="女" checked="true" name="sex"/>女
				</c:if>
			</td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>年龄</th>                                                                                  
			<td><input type="text" name="age"  value="${item.age}"/></td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>手机</th>                                                                                  
			<td><input type="text" name="phone"  value="${item.phone}"/></td>                                                                                
		</tr>  
		<tr>                                                                                                                                                                           
			<th>地址</th>                                                                                  
			<td><input type="text" name="address"  value="${item.address}"/></td>                                                                                
		</tr>  
		 		                                                   
	                                                                                                                                                                            
		<tr >                                                                                              
			<td colspan="2" ><input type="submit"value="修改" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#">返回</a></td>                                               
		</tr>                                                                                      
	                                                                                                       
	</table>
	</form>                                                                                                 
	
</body>
</html>
展示结果

list页面
在这里插入图片描述

add页面
在这里插入图片描述

update页面
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值