Home > Web Front-end > HTML Tutorial > Java web page staticization_html/css_WEB-ITnose

Java web page staticization_html/css_WEB-ITnose

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-24 11:50:30
Original
1331 people have browsed it

1 Note

Static pages themselves are many times faster than dynamic pages, and dynamic pages always have to be queried in the database, which will slow down even more!

Page staticization is to save the HTML generated by the dynamic page to a file on the server. Then when the same request is made again, the dynamic page will no longer be executed, but the user will directly respond to the static page that was generated last time. . And static pages can also help search engines find you!

2 View book categories

Let’s first write a small example to view books in different categories. Then we will think about how to make dynamic pages static.

index.jsp

  <body><a href="<c:url value='/BookServlet'/>">全部图书</a><br/><a href="<c:url value='/BookServlet?category=1'/>">JavaSE分类</a><br/><a href="<c:url value='/BookServlet?category=2'/>">JavaEE分类</a><br/><a href="<c:url value='/BookServlet?category=3'/>">Java框架分类</a><br/>  </body>
Copy after login

public class BookServlet extends HttpServlet {	public void doGet(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		BookService bookService = new BookService();		List<Book> 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);	}}
Copy after login

show.jsp

<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>		<td>			<c:choose>				<c:when test="${book.category eq 1}"><p style="color:red;">JavaSE分类</p></c:when>				<c:when test="${book.category eq 2}"><p style="color:blue;">JavaEE分类</p></c:when>				<c:when test="${book.category eq 3}"><p style="color:green;">Java框架分类</p></c:when>			</c:choose>		</td>	</tr>  </c:forEach></table>
Copy after login

3 Analysis

Generate a static page when the user visits the page for the first time, and then redirect the request to the static page. When the user visits again, he will be redirected directly to the static page.

We need to generate static pages for different requests. For example, when a user accesses BookServlet?category=1, we need to generate a static page. When a user accesses BookServlet?category=2, we also need to generate a static page. That is, different parameters generate different static pages!

We can use category as key, the path of the static page as value, save it to a Map, and then save the Map to the ServletContext. When there is no corresponding static page, we generate a static page and then redirect to the static page. If a static page exists, then redirect directly.



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();	}}
Copy after login

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<String,String> map = (Map<String, String>) sc.getAttribute("pages");		if(map == null) {			map = new HashMap<String,String>();			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();	}}
Copy after login


Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template