html+xml+servlet 通讯录案例demo
首先导入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> <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='保存'/> ";
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="保存"/>
<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="保存"/>
<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> <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的更多相关文章
- 分享知识-快乐自己:Shrio 案例Demo概述
Shiro 权限认证核心: POM:文件: <!--shiro-all--> <dependency> <groupId>org.apache.shiro</ ...
- Jquery+ajax+json+servlet原理和Demo
Jquery+ajax+json+servlet原理和Demo 大致过程: 用户时间点击,触发js,设置$.ajax,开始请求.服务器响应,获取ajax传递的值,然后处理.以JSON格式返回给ajax ...
- 浅谈JavaWEB入门必备知识之Servlet入门案例详解
工欲善其事.必先利其器,想要成为JavaWEB高手那么你不知道servlet是一个什么玩意的话,那就肯定没法玩下去,那么servlet究竟是个什么玩意?下面,仅此个人观点并通过一个小小的案例来为大家详 ...
- redis系列:通过通讯录案例学习hash命令
前言 这一篇文章将讲述Redis中的hash类型命令,同样也是通过demo来讲述,其他部分这里就不在赘述了. 项目Github地址:https://github.com/rainbowda/learn ...
- web.xml servlet、servlet-mapping配置
Servlet常称为服务器端小程序,即运行在服务器端的程序,用于处理及响应客户的请求. Servlet类是个特殊的java类,继承于HttpServlet. --------------------- ...
- Struts 2相关配置与基本操作演示(案例Demo)
基本介绍 Struts 2 Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2 ...
- 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 ...
- Jersey的RESTful简单案例demo
REST基础概念: 在REST中的一切都被认为是一种资源. 每个资源由URI标识. 使用统一的接口.处理资源使用POST,GET,PUT,DELETE操作类似创建,读取,更新和删除(CRUD)操作. ...
- 数据存储(两)--SAX发动机XML记忆(附Demo)
Android SDK支撑SAX读取技术XML,SAX通过连续的读取方式来处理XML文件.这要求每个读数XML对应的事件触发,以处理该节点的文件的节点.以下是基于一个例子来告诉SAX使用: publi ...
随机推荐
- SpringBoot static修饰的字段/方法如何获取application.yml配置
SpringBoot的application.yml一种特殊的应用场景,一般我们获取application.yml的配置文件只要@Value就可以获取到值了,但是如果是static修饰的字段肯定就不能 ...
- javascript 元编程之 method_missing
javascript 元编程之 method_missing 引言 要说元编程 ruby 中技巧太多了,今天来写的这个技术也来自于 ruby 中的灵感. method_missing 这个在 ruby ...
- Phoenix实现分页查询
1 利用offset语法,官网的语法 ELECT * FROM TEST LIMIT 1000; SELECT * FROM TEST LIMIT 1000 OFFSET 100; 2 公式如下 SE ...
- 蓝牙App漏洞系列分析之二CVE-2017-0639
蓝牙App漏洞系列分析之二CVE-2017-0639 0x01 漏洞简介 Android本月的安全公告,修复了我们发现的另一个蓝牙App信息泄露漏洞,该漏洞允许攻击者获取 bluetooth用户所拥有 ...
- Beta冲刺——星期五
这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <作业要求的链接> 团队名称 飞猪们 这个作业的目标 剩余任务预估,分配任务(开发,测试等).按要求提交当天冲刺报告. ...
- Python库整理
库名称简介 Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主要用于在终端或浏览器端构建格式 ...
- Web界面开发必看!Kendo UI for jQuery编辑功能指南第一弹
Kendo UI for jQuery最新试用版下载 Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support f ...
- 使用Quartus进行功能仿真时出现“testbench_vector_input_file option does not exist”的解决方法
环境:本人使用的Quartus 18 Prime Standard Edition 1.新建一个vmf文件 添加Node或者Bus 2.点击Processing->Start->S ...
- Practical, Dynamic Visibility for Games(可实现动态显示技术)
Practical, Dynamic Visibility for Games(可实现动态显示技术) 原文地址 1引言 游戏场景越来越复杂,包含的内容越来越多,动态显示技术很需要. 本文介绍2种互补的 ...
- Spring Boot 中application.yml与bootstrap.yml的区别(转)
yml与properties其实yml和properties文件是一样的原理,且一个项目上要么yml或者properties,二选一的存在. 推荐使用yml,更简洁. bootstrap与applic ...