日期:2014-05-17  浏览次数:20696 次

java-servlet、DOM、html综合运用示例
题目:

学员管理:
  学员录入,与Student类中字段对应,点击保存数据,提交给UserSaveServlet进行处理,保存成功后,显示成功信息,并且提供一个用户列表<a>的超链接,
点击“用户列表”超链接,显示出所有的用户数据信息,并且可以对数据进行删除操作。

说明:没有用到jsp,还没学到那儿……

学生信息录入页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="/StuRecServlet" method="post">
		姓名:<input type="text" name="name"/><br/>
		性别:<input type="radio" name="sex" value="true"/>男
			<input type="radio" name="sex" value="false"/>女<br/>
		年龄:<input type="text" name="age" /><br/>
		班级:<input type="text" name="className" /><br/>
		<input type="submit" value="提交信息" />
	</form>
</body>
</html>


Servlet项目配置信息:
<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app 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_2_5.xsd"
   version="2.5">	
	<servlet>
		<servlet-name>StuRecServlet</servlet-name>
		<servlet-class>cn.itcast.servlet.student.StuRecServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>StuRecServlet</servlet-name>
		<url-pattern>/StuRecServlet</url-pattern>
	</servlet-mapping>
	
	<servlet>
		<servlet-name>StuManServlet</servlet-name>
		<servlet-class>cn.itcast.servlet.student.StuManServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>StuManServlet</servlet-name>
		<url-pattern>/StuManServlet</url-pattern>
	</servlet-mapping>
	
	<servlet>
		<servlet-name>StuDelServlet</servlet-name>
		<servlet-class>cn.itcast.servlet.student.StuDelServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>StuDelServlet</servlet-name>
		<url-pattern>/StuDelServlet</url-pattern>
	</servlet-mapping>
</web-app>




学生domain和DAO:
package cn.itcast.servlet.domain;

public class Student {
	private String id;
	private String name;
	private boolean sex;
	private String age;
	private String className;
	
	
	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 boolean isSex() {
		return sex;
	}
	public void setSex(boolean sex) {
		this.sex = sex;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", sex=" + sex
				+ ", age=" + age + ", className=" + className + "]";
	}
}

package cn.itcast.servlet.dao;

import java.util.List;

public interface IStudentDAO {
	List<Student> getAllStudents();
	void addStudent(Student stu);
	void deleteStudent(String id);
//	void updateStudent(Student stu);
}

package cn.itcast.servlet.dao;

import java.io.File;

public class StudentDAOImpl implements IStudentDAO {
	private final static String XMLFile = "D:\\Java\\WorkSpace\\MyWebApps\\webdoc\\student.xml";

	/**
	 * 获取Document对象.
	 * 
	 * @return
	 */
	public Document getDoc() {
		DocumentBuilder builder = null;
		Document doc = null;
		try {
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			d