jsp操作xml项目实例下载



jsp操作xml项目实例下载,xml保存数据信息。向xml文件添加书本信息。jsp结合javabean操作xml介绍:

BookDirectory.java源码:

package com.cn.jspxml;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class BookDirectory {
private static Properties properties;

private static BookDirectory bookDirectory = null;

private static final String file = “”;

private BookDirectory() throws Exception {
properties = null;

java.io.InputStream input = this.getClass().getClassLoader()
.getResourceAsStream(file);

if (input != null) {
try {
properties = new Properties();
properties.load(input);
input.close();
} catch (IOException e) {
properties = null;
e.printStackTrace();
throw new Exception();
} finally {
input = null;
}

} else {
throw new Exception();
}
}

public static BookDirectory getInstance() throws Exception {
if (null == bookDirectory) {
bookDirectory = new BookDirectory();
}
return bookDirectory;
}
public void setFile(String name, String filename) throws Exception {
try {
properties.put(name, filename);
String o = this.getClass().getClassLoader().getResource(file)
.getFile();
properties.store(new FileOutputStream(o), “”);
} catch (IOException e) {
throw new Exception();
}
}

public String getFileName(String note){
String name = null;
if(!properties.isEmpty()) {
name = properties.getProperty(note);
}
return name;
}

public int getSize(){
return properties.size();
}
}

BookInfo.java源码:


package com.cn.jspxml;

public class BookInfo {
private String name = null;
private String filename = null;
public BookInfo(String note,String name){
this.name = note;
this.filename = name;
}
public String getAname() {
return name;
}
public void setAname(String name) {
this.name = name;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}

}

部分jsp页面:

addBook.jsp’源码:

<%@ page language=”java” pageEncoding=”gb2312″%>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>My JSP ‘addBook.jsp’ starting page</title>

</head>
<script>
function goto(){
var flag = “true”;
var bookname = document.frm.bookname.value;
if(bookname == “” ){
alert(“书名不能为空!”);
document.frm.bookname.focus();
flag = “false”;
}
var author = document.frm.author.value;
if(author == “” ){
alert(“作者不能为空!”);
document.frm.author.focus();
flag = “false”;
}
var press = document.frm.press.value;
if(press == “” ){
alert(“出版社不能为空!”);
document.frm.press.focus();
flag = “false”;
}
var keywords = document.frm.keywords.value;
if(keywords == “” ){
alert(“关键字不能为空!”);
document.frm.keywords.focus();
flag = “false”;
}
var content = document.getElementById(“content”).innerHTML;
if(content == “” ){
alert(“内容不能为空!”);
document.frm.content.focus();
flag = “false”;
}
var moban_id;
for(var i=0;i<2;i++) {
if(document.frm.typenum[i].checked) moban_id=document.frm.typenum[i].value;
}
if(moban_id == “” ){
alert(“模板不能为空!”);
flag = “false”;
}
if(flag == “true”){
location.href = “SaveBookToXMLServlet?bookname=”+bookname+”&author=”+author+”&press=”+press+”&keywords=”+keywords+”&content=”+content+”&typenum=”+moban_id;
}
}
</script>
<body>
<h1>添加书籍</h1>
<form name=”frm”>
<table border=”1″ width=”450px”>
<tr>
<td>书名</td>
<td><input type=”text” name=”bookname”></td>
</tr>
<tr>
<td>作者</td>
<td><input type=”text” name=”author”></td>
</tr>
<tr>
<td>出版社</td>
<td><input type=”text” name=”press”></td>
</tr>
<tr>
<td>书籍关键词</td>
<td><input type=”text” name=”keywords”></td>
</tr>
<tr>
<td>书籍概要</td>
<td><textarea id=”content” rows=”10″ cols=”50″></textarea></td>
</tr>
<tr>
<td>书籍类型</td>
<td><input type=”radio” name=”typenum” value=”article1″>书籍类型1
<input type=”radio” name=”typenum” value=”article2″>书籍类型2</td>
</tr>
</table>
<br>
<input type=”button” value=”添加书籍” onclick=”goto()”>
</form>

</body>
</html>

showBook.jsp

<%@ page language=”java” pageEncoding=”gb2312″%>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>My JSP ‘showBook.jsp’ starting page</title>
</head>

<body>
<div align=”center”>
<h1>添加数据到XML文件中</h1>
<br>
<br>
<a href=”addBook.jsp”><font color=”blue” size=”5″><b>添加书籍</b></font></a>
</body>
</html>

jsp操作xml文件项目下载