package com.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.pojo.Book;
public class BookService {
static String driver;
static String url;
static String username;
static String password;
public BookService(){
try{
Properties prop = new Properties();
prop.load(this.getClass().getResourceAsStream("db.properties"));
driver = prop.getProperty("driver");
url = prop.getProperty("url");
username = prop.getProperty("username");
password = prop.getProperty("password");
}catch(Exception e){
e.printStackTrace();
}
}
public List getBookList(){
List list = new ArrayList();
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from book");
while(rs.next()){
Book book = new Book();
book.setIsbn(rs.getString("isbn"));
book.setTitle(rs.getString("title"));
book.setPrice(rs.getString("price"));
list.add(book);
}
}catch(Exception e){
e.printStackTrace();
}
return list;
}
}
package com.tag;
import java.io.IOException;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import com.pojo.Book;
public class BookTagHandler extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
JspWriter out = this.getJspContext().getOut();
List<Book> list = (List<Book>)this.getJspContext().findAttribute("list");
for(Book o: list){
out.println(o.getIsbn() + "," + o.getTitle() + "," + o.getPrice() + "<br>");
}
}
}
package com.web;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.service.BookService;
public class BookAction extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//call business code
BookService bookService = new BookService();
List list = bookService.getBookList();
request.getSession().setAttribute("list", list);
request.getRequestDispatcher("listbookExec1.jsp").forward(request, response);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>BookAction</servlet-name>
<servlet-class>com.web.BookAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookAction</servlet-name>
<url-pattern>/BookAction</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>Application Tag Library</shortname>
<uri>http://jakarta.apache.org/taglibs/struts-example-1.0</uri>
<info></info>
<tag>
<name>display</name>
<tagclass>com.tag.BookTagHandler</tagclass>
<bodycontent>empty</bodycontent>
<info></info>
</tag>
</taglib>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/book.tld" prefix="book" %>
<HTML>
<HEAD>
<TITLE></TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="style/mycss.css" rel="stylesheet" type="text/css" />
<link href="style/texts.css" rel="stylesheet" type="text/css" />
<link href="style/btn.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0
MARGINHEIGHT=0>
<table width="780" border="0" align="center" cellpadding="0"
cellspacing="0">
<tr height="33">
<td colspan="5">
<br>
</td>
</tr>
<tr>
<td colspan="5">
<img src="images/top.jpg" width="771" height="23">
</td>
</tr>
<tr>
<td width="19" background="images/middle2.jpg">
</td>
<td width="695">
<table>
<tr>
<td width="3%">
</td>
<td width="97%">
<table width="688">
<tr>
<TD width='20%' height="30" align='center'>
ISBN
</TD>
<TD width='40%' align='center'>
TITLE
</TD>
<TD align='center' width="10%">
COPYRIGHT
</TD>
<TD width='10%' align='center'>
EDITIONNUMBER
</TD>
<TD align='center' width="20%">
PRICE
</TD>
</tr>
<tr>
<td colspan='5'>
<hr />
</td>
</tr>
<tr>
<td colspan='5'>
<book:display/>
</td>
</tr>
<tr>
<td colspan='5'>
<hr />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">@</td>
</tr>
</table>
</td>
<td width="40" background="images/middle4.jpg">
</td>
</tr>
<tr>
<td colspan="5" >
<img src="images/bottom.jpg" width="754" height="23">
</td>
</tr>
</table>
<P align="center">
2010 XXX Information Technology Co.,Ltd 版权所有
</P>
<br />
</BODY>
</HTML>