图书管理在页面静态化及处理乱码问题

图书管理
    页面
    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>

猜你喜欢

转载自blog.csdn.net/Entermomem/article/details/83891430
今日推荐