显示用户信息:
package gz.itcast.contactSys_web.servlet;
import gz.itcast.contactSys_web.dao.ContactDao;
import gz.itcast.contactSys_web.dao.impl.ContactDaoImpl;
import gz.itcast.contactSys_web.entity.Contact;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 显示所有联系人的逻辑
* @author APPle
*
*/
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>"; //火狐以get方式提交参数,而这个参数就会提交两次
html += " <td><a href='"+request.getContextPath()+"/QueryContactServlet?id="+contact.getId()+"'>修改</a> <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);
}
}
添加联系人:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>添加联系人</title>
</head>
<body>
<center><h3>添加联系人</h3></center>
<form action="/contactSys_web/AddContactServlet" method="post">
<table align="center" border="1" width="300px">
<tr>
<th>姓名</th>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<th>性别</th>
<td>
<input type="radio" name="gender" value="男"/>男
<input type="radio" name="gender" 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="email"/></td>
</tr>
<tr>
<th>QQ</th>
<td><input type="text" name="qq"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="保存"/>
<input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</body>
</html>
接收添加来的信息:
package gz.itcast.contactSys_web.servlet;
import gz.itcast.contactSys_web.dao.ContactDao;
import gz.itcast.contactSys_web.dao.impl.ContactDaoImpl;
import gz.itcast.contactSys_web.entity.Contact;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 添加联系人的逻辑
* @author APPle
*
*/
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);
}
}
修改联系人页面:
package gz.itcast.contactSys_web.servlet;
import gz.itcast.contactSys_web.dao.ContactDao;
import gz.itcast.contactSys_web.dao.impl.ContactDaoImpl;
import gz.itcast.contactSys_web.entity.Contact;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 修改前查询联系人的逻辑
* @author APPle
*
*/
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='保存'/> ";
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);
}
}
接收修改联系人信息:
package gz.itcast.contactSys_web.servlet;
import gz.itcast.contactSys_web.dao.ContactDao;
import gz.itcast.contactSys_web.dao.impl.ContactDaoImpl;
import gz.itcast.contactSys_web.entity.Contact;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 修改联系人的逻辑
* @author APPle
*
*/
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);
}
}
删除联系人:
package gz.itcast.contactSys_web.servlet;
import gz.itcast.contactSys_web.dao.ContactDao;
import gz.itcast.contactSys_web.dao.impl.ContactDaoImpl;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 删除联系人的逻辑
* @author APPle
*
*/
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);
}
}
方法的接口:
package gz.itcast.contactSys_web.dao;
import gz.itcast.contactSys_web.entity.Contact;
import java.util.List;
/**
* 联系人的DAO接口
* @author APPle
*
*/
public 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);//根据编号查询联系人
}
接口的实现:
package gz.itcast.contactSys_web.dao.impl;
import gz.itcast.contactSys_web.dao.ContactDao;
import gz.itcast.contactSys_web.entity.Contact;
import gz.itcast.contactSys_web.util.XMLUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class ContactDaoImpl implements ContactDao {
/**
* 添加联系人
*/
public void addContact(Contact contact) {
try {
File file = new File("d:/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){ //火狐会调用两次id,第一次有值,第二次为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);
}
}
读取文档和写入文档的工具类:
package gz.itcast.contactSys_web.util;
import java.io.File;
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
* xml的工具类
* @author APPle
*
*/
public class XMLUtil {
/**
* 读取xml文档方法
* @return
*/
public static Document getDocument(){
try {
Document doc = new SAXReader().read(new File("d:/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("d:/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);
}
}
}
测试类:
package gz.itcast.contactSys_web.test;
import gz.itcast.contactSys_web.dao.ContactDao;
import gz.itcast.contactSys_web.dao.impl.ContactDaoImpl;
import gz.itcast.contactSys_web.entity.Contact;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
/**
* 联系人操作实现类的测试类
* @author APPle
*
*/
public class TestContactOperatorImpl {
ContactDao operator = null;
/**
* 初始化对象实例
*/
@Before
public void init(){
operator = new ContactDaoImpl();
}
@Test
public void testAddContact(){
Contact contact = new Contact();
//contact.setId("2");
contact.setName("张三2");
contact.setGender("男");
contact.setAge(20);
contact.setPhone("134222233333");
contact.setEmail("eric@qq.com");
contact.setQq("33334444");
operator.addContact(contact);
}
@Test
public void testUpdateContact(){
Contact contact = new Contact();
contact.setId("1"); //修改的ID
contact.setName("李四");
contact.setGender("女");
contact.setAge(30);
contact.setPhone("135222233333");
contact.setEmail("zhangsan@qq.com");
contact.setQq("33334444");
operator.updateContact(contact);
}
@Test
public void testDeleteContact(){
operator.deleteContact("2");
}
@Test
public void testFindAll(){
List<Contact> list = operator.findAll();
for (Contact contact : list) {
System.out.println(contact);
}
}
@Test
public void testFindById(){
Contact contact = operator.findById("1");
System.out.println(contact);
}
}
实体对象:
package gz.itcast.contactSys_web.entity;
/**
* 实体对象
* @author APPle
*
*/
public 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 + "]";
}
}