1 说明
立即学习“Java免费学习笔记(深入)”;
静态页面本身就比动态页面快很多倍,而且动态页面总是要去数据库查询,这会更加降低速度!
页面静态化是把动态页面生成的html保存到服务器的文件上,然后再有相同请求时,不再去执行动态页面,而是直接给用户响应上次已经生成的静态页面。而且静态页面还有助与搜索引擎找到你!
2 查看图书分类
立即学习“Java免费学习笔记(深入)”;
我们先来写一个小例子,用来查看不同分类的图书。然后我们再去思考如何让动态页面静态化的问题。
index.jsp
立即学习“Java免费学习笔记(深入)”;
<a href="https://www.php.cn/faq/">全部图书
<a href="https://www.php.cn/faq/">JavaSE分类
<a href="https://www.php.cn/faq/">JavaEE分类
<a href="https://www.php.cn/faq/">Java框架分类
登录后复制
public class BookServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {BookService bookService = new BookService();List bookList = null;String param = request.getParameter("category");if(param == null || param.isEmpty()) {bookList = bookService.findAll();} else {int category = Integer.parseInt(param);bookList = bookService.findByCategory(category);}request.setAttribute("bookList", bookList);request.getRequestDispatcher("/show.jsp").forward(request, response);}}
登录后复制
show.jsp
立即学习“Java免费学习笔记(深入)”;
立即学习“Java免费学习笔记(深入)”;
登录后复制
图书名称 图书单价 图书分类${book.bname }${book.price }
JavaSE分类
JavaEE分类
Java框架分类
3 分析
立即学习“Java免费学习笔记(深入)”;
立即学习“Java免费学习笔记(深入)”;
用户第一次访问页面时生成静态页面,然后让请求重定向到静态页面上去。当用户再次访问时,直接重定向到静态页面上去。
我们需要为不同的请求生成静态页面,例如用户访问BookServlet?category=1时,我们要生成静态页面,当用户访问BookServlet?category=2时,也要生成静态页面。即不同的参数生成不同的静态页面!
我们可以使用category为key,静态页面的路径为value,保存到一个Map中,然后再把Map保存到ServletContext中。没有对应的静态页面时,我们生成静态页面,再重定向到静态页面,如果存在静态页面,那么直接重定向即可。
立即学习“Java免费学习笔记(深入)”;
public class StaticResponse extends HttpServletResponseWrapper {private PrintWriter pw;public StaticResponse(HttpServletResponse response, String filepath)throws FileNotFoundException, UnsupportedEncodingException {super(response);pw = new PrintWriter(filepath, "UTF-8");}public PrintWriter getWriter() throws IOException {return pw;}public void close() throws IOException {pw.close();}}
登录后复制
public class StaticFilter implements Filter {private ServletContext sc;public void destroy() {}public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {HttpServletRequest req = (HttpServletRequest) request;HttpServletResponse res = (HttpServletResponse) response;String key = "key_" + request.getParameter("category");Map map = (Map) sc.getAttribute("pages");if(map == null) {map = new HashMap();sc.setAttribute("pages", map);}if(map.containsKey(key)) {res.sendRedirect(req.getContextPath() + "/staticPages/" + map.get(key));return;}String html = key + ".html";String realPath = sc.getRealPath("/staticPages/" + html);StaticResponse sr = new StaticResponse(res, realPath);chain.doFilter(request, sr);sr.close();res.sendRedirect(req.getContextPath() + "/staticPages/" + html);map.put(key, html);}public void init(FilterConfig fConfig) throws ServletException {this.sc = fConfig.getServletContext();}}
登录后复制
立即学习“Java免费学习笔记(深入)”;
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/3098296.html