这个小项目用到的全部都是前面提到的只是,所以就不详细的讲解了,就讲一下之前没有提到的细节吧。
要注意的细节是:
这里我使用了xml文件来保存用户信息
1、我在这里进行编辑时是将xml文件放在Myeclipse的项目的src下面,而等到项目发布到tomcat上时,若你在代码中有关于其路径的设置,那么要注意了,发布以后xml文件会自动发布到tomcat下的WEB-INF下的classes文件夹里面,tomcat在调用时也是到这里来找,而不是到src那里去找。按照这种方式项目发布以后假如假如对用户信息进行过增删改查操作,会保存到WEB-INF下的classes里面的xml文件中,而不会保存到src下,即src下的xml文件不会有变化。当项目重新发布后,WEB-INF/classes的xml文件会被src下的冲掉。
2、当然,这个xml文件也可以放到电脑硬盘下的任何其他地方,即在WEB-INF/classes下不会有这个文件,那么你只需要在需要用到这个文件时把它所在的地址用上来就行了,但是假如这个项目有一天是要给用户使用的,你还是最好使用上一种方法好,因为用户不知道要去哪里新建一个文件,或者不新建而没办法改路径的。
代码如下:
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>servlet演示</title>
</head>
<body>
<h2>以下用于演示一个小项目--用户注册</h2>
<h3>用户注册</h3>
<form action="RegServlet" method="post">
姓名:<input type="text" name="name"><br/>
密码:<input type="password" name="pwd"><br/>
<input type="submit" value="注册">
</form>
<br/>
<a href="RegServlet">显示所有用户</a>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<servlet>
<servlet-name>RegServlet</servlet-name>
<servlet-class>cn.hncu.servlets.day2.RegServlet</servlet-class>
<init-param>
<param-name>charset</param-name>
<param-value>utf-8</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>DelServlet</servlet-name>
<servlet-class>cn.hncu.servlets.day2.DelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegServlet</servlet-name>
<url-pattern>/RegServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DelServlet</servlet-name>
<url-pattern>/DelServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
src下的users.xml
<?xml version="1.0" encoding="UTF-8"?>
<users>
</users>
这里使用到了第三方的dom4j,因此需要在WEB-INF/LIB下加入两个jar包:jaxen-1.1-beta-6.jar和dom4j-1.6.1.jar
package cn.hncu.factory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Dom4jFactory {
private static Document dom=null;
private static String path;
static {
try {
SAXReader sax=new SAXReader();
// 学习一下服务器下的资源路径加载方式(因为我们的资源已经从MyEclipse中发布到Tomcat服务器中了,所以跟原来纯Java项目不一样了)
// 利用当前类找到它的类加载器,然后通过该类加载器再去获得资源路径
path=Dom4jFactory.class.getClassLoader().getResource("users.xml").getPath();
dom=sax.read(new FileInputStream(path));
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static Document getDom(){
return dom;
}
public static void save(){
try {
XMLWriter w=new XMLWriter(new FileOutputStream(new File(path)));
w.write(dom);
w.close();
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static void main(String args[]){
System.out.println(getDom());
}
}
RegServlet.java
package cn.hncu.servlets.day2;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import cn.hncu.factory.Dom4jFactory;
public class RegServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String charset=this.getInitParameter("charset");
response.setContentType("text/html;charset="+charset);
PrintWriter writer=response.getWriter();
Document dom=Dom4jFactory.getDom();
List<Element> list=dom.selectNodes("//user");
writer.print("<html><body>");
for (Element e:list){
String id=e.attributeValue("id");
String del="<a href='DelServlet?id="+id+"'>删除</a>";
writer.print("name:"+e.attributeValue("name")+",pwd:"+e.attributeValue("pwd")+del+"<br/>");
}
writer.print("<a href='javascript:history.go(-1)'>返回</a>");
writer.print("</body></html>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String charset=this.getInitParameter("charset");
request.setCharacterEncoding(charset);
response.setContentType("text/html;charset="+charset);
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
Document dom=Dom4jFactory.getDom();
//防护,如果用户已经存在,给前端发送换一个的信息
Node node = dom.selectSingleNode("//user[@name='"+name.trim()+"']");
if (node!=null){
response.getWriter().print("该用户名已存在,请换一个");
} else {
Element root=dom.getRootElement();
Element elem=root.addElement("user");
elem.addAttribute("id", UUID.randomUUID().toString());
elem.addAttribute("name", name);
elem.addAttribute("pwd", pwd);
Dom4jFactory.save();
response.getWriter().print("注册成功");
response.getWriter().print(" <a href='javascript:history.go(-1)'>返回</a>");
}
}
}
DelServlet.java
package cn.hncu.servlets.day2;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dom4j.Document;
import org.dom4j.Node;
import cn.hncu.factory.Dom4jFactory;
public class DelServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String id=req.getParameter("id");
Document dom=Dom4jFactory.getDom();
Node node=dom.selectSingleNode("//user[@id='"+id+"']");
if (node!=null){
node.getParent().remove(node);
Dom4jFactory.save();
}
resp.sendRedirect("/servletDemo_mine/index.jsp");
}
}