JdbcUtil.java
package util; import com.mchange.v2.c3p0.ComboPooledDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; public class JdbcUtil { // 需要配置c3p0-config.xml private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); // 返回连接对象 public static Connection getConnection() { try { return dataSource.getConnection(); } catch (SQLException e) { throw new RuntimeException(e); } } // 返回连接池对象 public static DataSource getDataSource() { return dataSource; } // 释放连接 public static void releaseConnection(Connection connection) { try { connection.close(); } catch (SQLException e) { throw new RuntimeException(e); } } }
book.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <h2>图书列表</h2> 分类: <a href="book?method=findAll">全部</a> <a href="book?method=findByCategory&category=1">第一类</a> <a href="book?method=findByCategory&category=2">第二类</a> <table border="1"> <tr> <th>ID</th> <th>书名</th> <th>价格</th> <th>分类</th> </tr> <c:forEach items="${bookList}" var="book"> <tr> <td>${book.bid}</td> <td>${book.bname}</td> <td>${book.price}</td> <td>${book.category}</td> </tr> </c:forEach> </table>
配置文件
pom.xml
<dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency>
web.xml
<servlet-mapping> <servlet-name>BookServlet</servlet-name> <url-pattern>/book</url-pattern> </servlet-mapping> <servlet> <servlet-name>BookServlet</servlet-name> <servlet-class>com.pengshiyu.servlet.BookServlet</servlet-class> </servlet>
c3p0-config.xml
<?xml version="1.0" encoding="utf-8"?> <c3p0-config> <!-- 这是默认配置信息 --> <default-config> <!-- 连接四大参数配置 --> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/data</property> <property name="user">root</property> <property name="password">123456</property> <!-- 池参数配置 --> <property name="acquireIncrement">2</property> <property name="initialPoolSize">2</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </default-config> </c3p0-config>
访问路径
http://localhost:8080/demo/book?method=findAll
http://localhost:8080/demo/book?method=findByCategory&category=1
课时10 案例4:页面静态化之如果文件存在直接重定向到html
使用一个过滤器,把servlet请求的资源输出保存到html中
第二次访问资源的时候,如果已存在就直接重定向到html文件
课时11 案例5:页面静态之生成html页面
CacheFilter.java
package com.pengshiyu.filter; import com.pengshiyu.response.StaticResponse; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; public class CacheFilter implements Filter { private FilterConfig config; private final String cacheFileName = "cache"; private String cacheFilePath = null; @Override public void init(FilterConfig filterConfig) throws ServletException { this.config = filterConfig; this.cacheFilePath = this.config.getServletContext().getRealPath(this.cacheFileName); File file = new File(this.cacheFilePath); if(file.exists()){ file.mkdir(); } } /** * 访问路径 * http://localhost:8080/demo/book?method=findByCategory&category=4 */ @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)servletRequest; HttpServletResponse response = (HttpServletResponse)servletResponse; String category = request.getParameter("category"); String filepath = this.cacheFilePath + "/" + category + ".html"; File file = new File(filepath); // 如果页面不存在就缓存页面 if(!file.exists()){ StaticResponse staticResponse = new StaticResponse(response, filepath); filterChain.doFilter(request, staticResponse); } System.out.println("文件存在了"); request.getRequestDispatcher(this.cacheFileName + "/" + category + ".html").forward(request, response); } @Override public void destroy() { } }
StaticResponse.java
package com.pengshiyu.response; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; public class StaticResponse extends HttpServletResponseWrapper { private PrintWriter pw; public StaticResponse(HttpServletResponse response, String filename) throws FileNotFoundException { super(response); this.pw = new PrintWriter(filename); } @Override public PrintWriter getWriter() throws IOException { // 掉包输出流 return this.pw; } }