这个学期要学EJB了,之前的servlet学习是少不了的。其实发现我们不少人,不大重视SERVLET的学习,好象因为框架,JSP之类都把
servlet的编写减少到最低限度了,但还是要注重实质的,于是找了本*人写的《SCWCD学习》(SCWCD是JAVA认证的第二类考试,
比SCJP高一个级),发现还是*人写的书好懂,于是笔记关键之,并学习之。
举例,有一个文本文件,存放书本的详细信息,如书本,作者,出版社等信息,每本书之间用 “|”分隔符隔开,放在一个booklist.txt文件中, 由于WEB应用程序要读取这些数据,所以WEB应用程序在初始化阶段就要初始化书籍,就是在servlet运行之前,WEB应用程序必须从booklist.txt 文件中读取书的信息,然后建立book对象,然后当servlet运行时就可以读出所有书的数据。因此,应该建立一个监听器。
监听器为:
public class InitializeBookList implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce)
{
//获得WEB应用程序的ServletContext对象
ServletContext context=sce.getServletContext();
//通过context.getInitParameter()取得BookListFile参数值,该参数值在web.xml
String booklist=context.getInitParameter("BookListFile");
InputStream is=null;
BufferedReader br=null;
try
{
is=context.getResourceAsStream(booklist);
br=new BufferedReader(new InputStreamReader(is));
String bookDetails; // 存放从 booklist.txt 读取的每一条数据
GoodBooks books = new GoodBooks();
// 依序取出“书名”、“作者”、“出版社”、“ISBN”、“售价”等数据
while ( (bookDetails = br.readLine()) != null )
{
StringTokenizer tokens = new StringTokenizer(bookDetails, "|");
String name = tokens.nextToken();
String author = tokens.nextToken();
String publisher = tokens.nextToken();
String isbn = tokens.nextToken();
String price = tokens.nextToken();
// 建立一个 Book 对象
Book b = new Book(name, author, publisher, isbn, price);
// 将 Book 对象加入 books 对象(books对象将包含多本书籍数据)
books.addBook(b);
}
//将books对象存入ServletContext对象的属性(allbooks)
context.setAttribute("allBooks",books);
context.log("OK");
is.close();
br.close();
}
catch (Exception e)
{
context.log("Web 应用程序初始化发生问题!!", e);
}
}
public void contextDestroyed(ServletContextEvent sce) {}
}
BOOK.JAVA
public class Book {
private String name;
private String author;
private String publisher;
private String isbn;
private String price;
public Book(String name, String author,String publisher,
String isbn, String price ) {
this.name = name;
this.author = author;
this.publisher = publisher;
this.isbn = isbn;
this.price = price;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public String getPublisher() {
return publisher;
}
public String getIsbn() {
return isbn;
}
public String getPrice() {
return price;
}
}
goodbooks.java
public class GoodBooks {
private List BooksList;
public GoodBooks() {
BooksList = new LinkedList();
}
public void addBook(Book book) {
BooksList.add(book);
}
public Iterator getBookList() {
return BooksList.iterator();
}
}
最后显示书本列表的servlet.java
public class ShowGoodBooks extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
ServletContext context = getServletContext();
// 从 ServletContext 对象取出 allBooks 属性
// (参考 InitializeBookList.java 第 43 行)
GoodBooks books = (GoodBooks) context.getAttribute("allBooks");
Iterator items = books.getBookList();
response.setContentType("text/html;charset=GB2312");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Java 好书推荐</TITLE>");
out.println("</HEAD>");
out.println("<H2>Java 好书推荐</H2>");
out.println("<TABLE border=1 WIDTD=100%");
// 跃富
out.println("<TR>");
out.println("<TD bgcolor=#C0C0C0>书名</TD>");
out.println("<TD bgcolor=#C0C0C0>作者</TD>");
out.println("<TD bgcolor=#C0C0C0>出版社</TD>");
out.println("<TD bgcolor=#C0C0C0>ISBN</TD>");
out.println("<TD bgcolor=#C0C0C0>售价</TD>");
out.println("</TR>");
// 显示每一条书籍数据
while ( items.hasNext() ) {
Book book = (Book) items.next();
out.println("<TR>");
out.println("<TD>" + book.getName() + "</TD>");
out.println("<TD>" + book.getAuthor() + "</TD>");
out.println("<TD>" + book.getPublisher() + "</TD>");
out.println("<TD>" + book.getIsbn() + "</TD>");
out.println("<TD>" + book.getPrice() + "</TD>");
out.println("</TR>");
}
out.println("</TABLE>");
out.println("</BODY>");
out.println("</HTML>");
}
}