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即可
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!