圖書管理在頁面靜態化及處理亂碼問題
阿新 • • 發佈:2018-11-19
圖書管理 頁面 jsp:link.jsp 連結頁面,四個超連結 查詢所有 檢視SE分類 檢視EE分類 檢視框架分類 show.jsp 顯示查詢結果 Servlet: BookServlet findAll()-->檢視所有圖書 findByCateqory->按分類進行查詢 BookService 略 BookDao: List<Book> findAll() List<Book> findByCateqory(int cateqory) domain: Book類 2、第二步:什麼是頁面靜態化 首次訪問去資料庫獲取資料,然後把資料儲存到一個html頁面中 二次訪問,就不再去資料庫獲取了,而是直接顯示html 頁面靜態化 1、目標: 給出一個過濾器,把servlet請求的資源所做的輸出儲存到html中,重定向到html頁面 二次訪問時,這個html已經存在,那麼直接重定向,不用再去訪問servlet
public class BookDao { private QueryRunner qr = new TxQueryRunner(); public List<Book> findAll() { try { String sql = "select * from t_book"; return qr.query(sql, new BeanListHandler<Book>(Book.class)); } catch (SQLException e) { throw new RuntimeException(e); } } public List<Book> findByCateqory(int cateqory) { try { String sql = "select * from t_book where cateqory=?"; return qr.query(sql, new BeanListHandler<Book>(Book.class), cateqory); } catch (SQLException e) { throw new RuntimeException(e); } } }
public class StaticFilter implements Filter { private FilterConfig filterConfig; public StaticFilter() { } public void destroy() { } public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; /* *1、第一次訪問時,查詢請求對應的html頁面是否存在,如果存在重定向到html *2、如果不存在,放行,把servlet訪問資料庫後,輸出給客戶端的資料儲存到一個html檔案中 * 再重定向到html */ /* * 一、獲取category引數 * category有四種可能: * null-->null.html * 1-->1.html * 2-->2.html * 3-->3.html * * html頁面的儲存路徑,htmls目錄下 * 判斷對應的html檔案是否存在,如果存在,直接重定向 */ String category = req.getParameter("cateqory"); String htmlPage = category + ".html";//得到對應的檔名稱 String htmlPath = filterConfig.getServletContext().getRealPath("/htmls");//得到檔案的存放目錄 File destFile = new File(htmlPath,htmlPage); if(destFile.exists()){//如果檔案存在 //重定向到這個檔案 response.sendRedirect(request.getContextPath()+"/htmls/"+htmlPage); return; } /* * 二、如果html檔案不存在,我們要生成html * 1、放行,show.jsp會做出很多的輸出,別再輸出給客戶端,而是輸出到我們指定的一個html檔案中 * 完成 * 掉包response,讓它的getWriter()與一個html檔案繫結,那麼show.jsp就到了html檔案中 * */ StaticResponse sr = new StaticResponse(response, destFile.getAbsolutePath()); filterChain.doFilter(request, sr);//放行,即生成了html檔案 //這時頁面已經存在,重定向到html檔案 response.sendRedirect(request.getContextPath()+"/htmls/"+htmlPage); } public void init(FilterConfig fConfig) throws ServletException { this.filterConfig = fConfig; } }
public class StaticResponse extends HttpServletResponseWrapper {
private PrintWriter pw;
private HttpServletResponse response;
public StaticResponse(HttpServletResponse response,String path) throws FileNotFoundException, UnsupportedEncodingException {
super(response);
this.response = response;
pw = new PrintWriter(path,"utf-8");
}
public PrintWriter getWriter(){
//返回一個html繫結在一起的printWriter物件
//jsp會使用它進行輸出,這樣資料都輸出到html檔案中
return pw;
}
}
public class BookServlet extends BaseServlet {
private BookDao bookDao = new BookDao();
public String findAll(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("bookList", bookDao.findAll());
return "f:/show.jsp";
}
public String findByCateqory(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("cateqory");
int value = Integer.parseInt(name);
request.setAttribute("bookList", bookDao.findByCateqory(value));
return "f:/show.jsp";
}
}
show.jsp
編碼: <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<body>
<h1 align="center">圖書列表</h1>
<table border="1" align="center" width="50%">
<tr>
<th>書名</th>
<th>單價</th>
<th>分類</th>
</tr>
<c:forEach items="${bookList }" var="book">
<tr>
<td>${book.bname}</td>
<td>${book.price }</td>
<c:choose>
<c:when test="${book.cateqory eq 1 }">
<td style="color:gray;">JavaSE</td>
</c:when>
<c:when test="${book.cateqory eq 2 }">
<td style="color:blue">JavaEE</td>
</c:when>
<c:when test="${book.cateqory eq 3 }">
<td style="color:green">JavaFramework</td>
</c:when>
</c:choose>
</tr>
</c:forEach>
</table>
</body>
link.jsp
<body>
<h1 align="center">查詢</h1>
<table align="center">
<tr>
<td><a href="<c:url value='/BookServlet?method=findAll'/>">檢視所有圖書</a></td>
</tr>
<tr>
<td><a href="<c:url value='/BookServlet?method=findByCateqory&cateqory=1'/>">檢視SE</a></td>
</tr>
<tr>
<td><a href="<c:url value='/BookServlet?method=findByCateqory&cateqory=2'/>">檢視EE</a></td>
</tr>
<tr>
<td><a href="<c:url value='/BookServlet?method=findByCateqory&cateqory=3'/>">檢視framework</a></td>
</tr>
</table>
</body>