首先导入dom4j和xPath技术以及测试对应的jar包


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

import com.loaderman.demo.dao.ContactDao;
import com.loaderman.demo.dao.impl.ContactDaoImpl;
import com.loaderman.demo.entity.Contact; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 添加联系人的逻辑
*
*/
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()+"/listContact");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
package com.loaderman.demo.servlet;

import com.loaderman.demo.dao.ContactDao;
import com.loaderman.demo.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;
/**
* 删除联系人的逻辑 *
*/
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()+"/listContact");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
package com.loaderman.demo.servlet;

import com.loaderman.demo.dao.ContactDao;
import com.loaderman.demo.dao.impl.ContactDaoImpl;
import com.loaderman.demo.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;
/**
* 显示所有联系人的逻辑 *
*/
public class ListContactServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.从xml中读取出联系人数据
ContactDao dao = new ContactDaoImpl();
List<Contact> list = dao.findAll(); //2.显示到浏览器
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter(); String html = ""; //shift+alt+A ^(.*)$ \1";
html += "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
html += "<html xmlns='http://www.w3.org/1999/xhtml'>";
html += "<head>";
html += "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
html += "<title>查询所有联系人</title>";
html += "<style type='text/css'>";
html += " table td{";
html += " /*文字居中*/";
html += " text-align:center;";
html += " }";
html += " ";
html += " /*合并表格的边框*/";
html += " table{";
html += " border-collapse:collapse;";
html += " }";
html += "</style>";
html += "</head>";
html += "";
html += "<body>";
html += "<center><h3>查询所有联系人</h3></center>";
html += "<table align='center' border='1' width='800px'>";
html += " <tr>";
html += " <th>编号</th>";
html += " <th>姓名</th>";
html += " <th>性别</th>";
html += " <th>年龄</th>";
html += " <th>电话</th>";
html += " <th>邮箱</th>";
html += " <th>QQ</th>";
html += " <th>操作</th>";
html += " </tr>";
if(list!=null){
for (Contact contact : list) {
html += " <tr>";
html += " <td>"+contact.getId()+"</td>";
html += " <td>"+contact.getName()+"</td>";
html += " <td>"+contact.getGender()+"</td>";
html += " <td>"+contact.getAge()+"</td>";
html += " <td>"+contact.getPhone()+"</td>";
html += " <td>"+contact.getEmail()+"</td>";
html += " <td>"+contact.getQq()+"</td>";
html += " <td><a href='"+request.getContextPath()+"/queryContact?id="+contact.getId()+"'>修改</a>&nbsp;<a href='"+request.getContextPath()+"/deleteContact?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);
} }
package com.loaderman.demo.servlet;

import com.loaderman.demo.dao.ContactDao;
import com.loaderman.demo.dao.impl.ContactDaoImpl;
import com.loaderman.demo.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;
/**
* 修改前查询联系人的逻辑 *
*/
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()+"/updateContact' method='post'>";
//注意:添加id的隐藏域
html += "<input type='hidden' name='id' value='"+contact.getId()+"'/>";
html += "<table align='center' border='1' width='300px'>";
html += " <tr>";
html += " <th>姓名</th>";
html += " <td><input type='text' name='name' value='"+contact.getName()+"'/></td>";
html += " </tr>";
html += " <tr>";
html += " <th>性别</th>";
html += " <td>"; if(contact.getGender().equals("男")){
html += " <input type='radio' name='gender' value='男' checked='checked'/>男";
html += " <input type='radio' name='gender' value='女'/>女";
}else if(contact.getGender().equals("女")){
html += " <input type='radio' name='gender' value='男'/>男";
html += " <input type='radio' name='gender' value='女' checked='checked'/>女";
}else{
html += " <input type='radio' name='gender' value='男' checked='checked'/>男";
html += " <input type='radio' name='gender' value='女'/>女";
} html += " </td>";
html += " </tr>";
html += " <tr>";
html += " <th>年龄</th>";
html += " <td><input type='text' name='age' value='"+contact.getAge()+"'/></td>";
html += " </tr>";
html += " <tr>";
html += " <th>电话</th>";
html += " <td><input type='text' name='phone' value='"+contact.getPhone()+"'/></td>";
html += " </tr>";
html += " <tr>";
html += " <th>邮箱</th>";
html += " <td><input type='text' name='email' value='"+contact.getEmail()+"'/></td>";
html += " </tr>";
html += " <tr>";
html += " <th>QQ</th>";
html += " <td><input type='text' name='qq' value='"+contact.getQq()+"'/></td>";
html += " </tr>";
html += " <tr>";
html += " <td colspan='2' align='center'>";
html += " <input type='submit' value='保存'/>&nbsp;";
html += " <input type='reset' value='重置'/></td>";
html += " </tr>";
html += "</table>";
html += "</form>";
html += "</body>";
html += "</html>"; writer.write(html);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
package com.loaderman.demo.servlet;

import com.loaderman.demo.dao.ContactDao;
import com.loaderman.demo.dao.impl.ContactDaoImpl;
import com.loaderman.demo.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;
/**
* 修改联系人的逻辑
*
*/
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()+"/listContact"); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
package com.loaderman.demo.test;

import com.loaderman.demo.dao.ContactDao;
import com.loaderman.demo.dao.impl.ContactDaoImpl;
import com.loaderman.demo.entity.Contact;
import org.junit.Before;
import org.junit.Test; import java.util.List; /**
* 联系人操作实现类的测试类
*
*/
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);
}
}

addContact.xml

<!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="/addContact" 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="保存"/>&nbsp;
<input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</body>
</html>

修改联系人.xml

<!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="查询所有联系人.html" method="post">
<table align="center" border="1" width="300px">
<tr>
<th>姓名</th>
<td><input type="text" name="name" value="张三"/></td>
</tr>
<tr>
<th>性别</th>
<td>
<input type="radio" name="gender" value="男"/>男
<input type="radio" name="gender" value="女" checked="checked"/>女
</td>
</tr>
<tr>
<th>年龄</th>
<td><input type="text" name="age" value="20"/></td>
</tr>
<tr>
<th>电话</th>
<td><input type="text" name="phone" value="13422223333"/></td>
</tr>
<tr>
<th>邮箱</th>
<td><input type="text" name="email" value="zs@qq.com"/></td>
</tr>
<tr>
<th>QQ</th>
<td><input type="text" name="qq" value="3333444"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="保存"/>&nbsp;
<input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</body>
</html>

listContact.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.loaderman.demo.entity.Contact" %>
<!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>
<style type="text/css">
table td{
/*文字居中*/
text-align:center;
} /*合并表格的边框*/
table{
border-collapse:collapse;
}
</style>
</head> <body>
<center><h3>查询所有联系人(jsp版本)</h3></center>
<table align="center" border="1" width="700px">
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>电话</th>
<th>邮箱</th>
<th>QQ</th>
<th>操作</th>
</tr>
<%
//从request域中接收数据
List<Contact> list = (List<Contact>)request.getAttribute("contacts"); for(Contact con:list){
%>
<tr>
<td><%=con.getId() %></td>
<td><%=con.getName() %></td>
<td><%=con.getGender() %></td>
<td><%=con.getAge() %></td>
<td><%=con.getPhone() %></td>
<td><%=con.getEmail() %></td>
<td><%=con.getQq() %></td>
<td><a href="修改联系人.html">修改</a>&nbsp;<a href="查询所有联系人.html">删除</a></td>
</tr>
<%
}
%>
<tr>
<td colspan="8" align="center"><a href="添加联系人.html">[添加联系人]</a></td>
</tr>
</table>
</body>
</html>

web,xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"> <!-- servlet的配置 -->
<servlet>
<!-- servlet的内部名称,自定义。尽量有意义 -->
<servlet-name>mTestServlet</servlet-name>
<!-- servlet的类全名: 包名+简单类名 -->
<servlet-class>com.loaderman.demo.TestServlet</servlet-class>
</servlet> <!-- servlet的映射配置 -->
<servlet-mapping>
<!-- servlet的内部名称,一定要和上面的内部名称保持一致!! -->
<servlet-name>mTestServlet</servlet-name>
<!-- servlet的映射路径(访问servlet的名称) -->
<url-pattern>/indexTest</url-pattern>
</servlet-mapping> <!-- servlet的配置 -->
<servlet>
<!-- servlet的内部名称,自定义。尽量有意义 -->
<servlet-name>AddContactServlet</servlet-name>
<!-- servlet的类全名: 包名+简单类名 -->
<servlet-class>com.loaderman.demo.servlet.AddContactServlet</servlet-class>
</servlet> <!-- servlet的映射配置 -->
<servlet-mapping>
<!-- servlet的内部名称,一定要和上面的内部名称保持一致!! -->
<servlet-name>AddContactServlet</servlet-name>
<!-- servlet的映射路径(访问servlet的名称) -->
<url-pattern>/addContact</url-pattern>
</servlet-mapping> <!-- servlet的配置 -->
<servlet>
<!-- servlet的内部名称,自定义。尽量有意义 -->
<servlet-name>DeleteContactServlet</servlet-name>
<!-- servlet的类全名: 包名+简单类名 -->
<servlet-class>com.loaderman.demo.servlet.DeleteContactServlet</servlet-class>
</servlet> <!-- servlet的映射配置 -->
<servlet-mapping>
<!-- servlet的内部名称,一定要和上面的内部名称保持一致!! -->
<servlet-name>DeleteContactServlet</servlet-name>
<!-- servlet的映射路径(访问servlet的名称) -->
<url-pattern>/deleteContact</url-pattern>
</servlet-mapping> <!-- servlet的配置 -->
<servlet>
<!-- servlet的内部名称,自定义。尽量有意义 -->
<servlet-name>ListContactServlet</servlet-name>
<!-- servlet的类全名: 包名+简单类名 -->
<servlet-class>com.loaderman.demo.servlet.ListContactServlet</servlet-class>
</servlet> <!-- servlet的映射配置 -->
<servlet-mapping>
<!-- servlet的内部名称,一定要和上面的内部名称保持一致!! -->
<servlet-name>ListContactServlet</servlet-name>
<!-- servlet的映射路径(访问servlet的名称) -->
<url-pattern>/listContact</url-pattern>
</servlet-mapping> <!-- servlet的映射配置 --> <servlet>
<!-- servlet的内部名称,自定义。尽量有意义 -->
<servlet-name>QueryContactServlet</servlet-name>
<!-- servlet的类全名: 包名+简单类名 -->
<servlet-class>com.loaderman.demo.servlet.QueryContactServlet</servlet-class>
</servlet> <!-- servlet的映射配置 -->
<servlet-mapping>
<!-- servlet的内部名称,一定要和上面的内部名称保持一致!! -->
<servlet-name>QueryContactServlet</servlet-name>
<!-- servlet的映射路径(访问servlet的名称) -->
<url-pattern>/queryContact</url-pattern>
</servlet-mapping>
<servlet>
<!-- servlet的内部名称,自定义。尽量有意义 -->
<servlet-name>UpdateContactServlet</servlet-name>
<!-- servlet的类全名: 包名+简单类名 -->
<servlet-class>com.loaderman.demo.servlet.UpdateContactServlet</servlet-class>
</servlet> <!-- servlet的映射配置 -->
<servlet-mapping>
<!-- servlet的内部名称,一定要和上面的内部名称保持一致!! -->
<servlet-name>UpdateContactServlet</servlet-name>
<!-- servlet的映射路径(访问servlet的名称) -->
<url-pattern>/updateContact</url-pattern>
</servlet-mapping> </web-app>

运行到服务器,请求addContact.html即可

html+xml+servlet 通讯录案例demo的更多相关文章

  1. 分享知识-快乐自己:Shrio 案例Demo概述

    Shiro 权限认证核心: POM:文件: <!--shiro-all--> <dependency> <groupId>org.apache.shiro</ ...

  2. Jquery+ajax+json+servlet原理和Demo

    Jquery+ajax+json+servlet原理和Demo 大致过程: 用户时间点击,触发js,设置$.ajax,开始请求.服务器响应,获取ajax传递的值,然后处理.以JSON格式返回给ajax ...

  3. 浅谈JavaWEB入门必备知识之Servlet入门案例详解

    工欲善其事.必先利其器,想要成为JavaWEB高手那么你不知道servlet是一个什么玩意的话,那就肯定没法玩下去,那么servlet究竟是个什么玩意?下面,仅此个人观点并通过一个小小的案例来为大家详 ...

  4. redis系列:通过通讯录案例学习hash命令

    前言 这一篇文章将讲述Redis中的hash类型命令,同样也是通过demo来讲述,其他部分这里就不在赘述了. 项目Github地址:https://github.com/rainbowda/learn ...

  5. web.xml servlet、servlet-mapping配置

    Servlet常称为服务器端小程序,即运行在服务器端的程序,用于处理及响应客户的请求. Servlet类是个特殊的java类,继承于HttpServlet. --------------------- ...

  6. Struts 2相关配置与基本操作演示(案例Demo)

    基本介绍 Struts 2        Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2 ...

  7. File Templates for web.xml & web-fragment.xml (Servlet 2.3, 2.4, 2.5 + 3.0)

    As I sometimes need these, I have compiled a list of the valid headers of the web.xml and web-fragme ...

  8. Jersey的RESTful简单案例demo

    REST基础概念: 在REST中的一切都被认为是一种资源. 每个资源由URI标识. 使用统一的接口.处理资源使用POST,GET,PUT,DELETE操作类似创建,读取,更新和删除(CRUD)操作. ...

  9. 数据存储(两)--SAX发动机XML记忆(附Demo)

    Android SDK支撑SAX读取技术XML,SAX通过连续的读取方式来处理XML文件.这要求每个读数XML对应的事件触发,以处理该节点的文件的节点.以下是基于一个例子来告诉SAX使用: publi ...

随机推荐

  1. Oracle笔记(四) 简单查询、限定查询、数据的排序

    一.简单查询 SQL(Structured Query Language) 结构化查询语言,是一种数据库查询和程序设计语言,用于存取数据以及查询.更新和管理关系数据库系统.ANSI(美国国家标准学会) ...

  2. kubernetes之service

    service出现的动机 Kubernetes Pods 是有生命周期的.他们可以被创建,而且销毁不会再启动. 如果您使用 Deployment 来运行您的应用程序,则它可以动态创建和销毁 Pod. ...

  3. Raspberrypi 安装完MySQL之后登录不了(ERROR 1698 (28000))

    1.问题原因: 出现这是错误是因为 mysql 默认的 root 用户使用了 UNIX auth_socket_plugin 的用户认证方式,我们有下面两种方式处理问题: 修改 root 用户认证方式 ...

  4. 解决Chrome无法安装CRX离线插件

    解释说明: 谷歌浏览器Chrome,版本号67.0.3396.99,自这个版本后的Chrome,手动拖放插件文件crx到谷歌浏览器,这种安装插件的方式,一定会失败,它会提示“无法从该网站添加应用,扩展 ...

  5. Python制作的射击游戏

    如果其他朋友也有不错的原创或译文,可以尝试推荐给伯乐在线.] 你有没有想过电脑游戏是怎样制作出来的?其实它没有你想象的那样复杂! 在这个教程里,你要学做一个叫<兔子和獾>的塔防游戏,兔子作 ...

  6. 《Python基础教程》第五章:条件、循环和其他语句

    在Python中赋值运算和比较运算是可以连接的,运算符可以连在一起使用,如:0<age<100 ==运算符判定两个对象是否相等,is判定两者是否等同(同一个对象) 断言,在错误条件出现时直 ...

  7. mybatic MapperScannerConfigurer的原理

    原文地址:http://www.cnblogs.com/fangjian0423/p/spring-mybatis-MapperScannerConfigurer-analysis.html 前言 本 ...

  8. Java-判断是否为回文数

    /** * @ClassName: IsPalindrome * @author: bilaisheng * @date: 2017年9月19日 下午2:54:08 * 判断是否为回文数 * true ...

  9. SpringMVC全局异常统一处理

    SpringMVC全局异常统一处理以及处理顺序最近在使用SpringMVC做全局异常统一处理的时候遇到的问题,就是想把ajax请求和普通的网页请求分开返回json错误信息或者跳转到错误页. 在实际做的 ...

  10. 【Winform-自定义控件】自定义控件学习+一个笑脸控件例子

    1.CompositeControls组合控件:在原有控件的基础上根据需要进行组合 2.ExtendedControls 扩展控件:继承自原有控件,添加一些新的属性和方法,绘制一些新元素 当每个But ...