虽然页面是动态的,要从数据库里取数据,但很多数据往往不经常改变,这些界面就可以做成静态的,可以极大提高效率。
主要思路就是:原本把数据从jsp翻译成servlet,然后打印给浏览器,但现在直接把response的反馈给浏览器的,直接放进html文件中,下次访问这个页面,只需要把请求转向 html 文件即可。
要修改Response,写一个修改Response成自己写的类,一个拦截器,拦截特定需要静态化的页面。
StaticFilter.java
package com.filter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class StaticFilter implements Filter {
FilterConfig config=null;
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest) req;
HttpServletResponse response=(HttpServletResponse) resp;
ServletContext context=config.getServletContext();
Map<String,String>map=(Map<String, String>) context.getAttribute("map");
if(map==null)
map=new HashMap<String,String>();
String category=request.getParameter("category");
String key=category+".html";
String path = map.get(key);
if(path==null||path.trim().isEmpty())
{
map.put(key, "/html/"+key);
context.setAttribute("map", map);
path = map.get(key);
StaticResponse staticResp=new StaticResponse(response,request.getRealPath("\\")+path.substring(1).replace("/", "\\"));
chain.doFilter(request, staticResp);
}
response.sendRedirect(request.getContextPath()+"/html/"+key);
return ;
}
public void init(FilterConfig config) throws ServletException {
this.config=config;
}
}
StaticResponse.javapackage com.filter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class StaticResponse extends HttpServletResponseWrapper {
HttpServletResponse resp;
PrintWriter pw;
public StaticResponse(HttpServletResponse response,String staticPath) {
super(response);
resp=response;
try {
pw=new PrintWriter(staticPath,"utf-8");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public PrintWriter getWriter() throws IOException {
return pw;
}
}
DataDao.java
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Dao;
public class DataDao extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String category = request.getParameter("category");
if(category==null||category.trim().isEmpty())
{
request.setAttribute("result", Dao.getAll());
}
else
{
request.setAttribute("result", Dao.getjava());
}
request.getRequestDispatcher("/show.jsp").forward(request, response);
}
}
show.jsp
<%@ page language="java" import="java.util.*,com.domain.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'show.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<c:forEach var="item" items="${result}">
<span style="color:${item.category==1?'red':'blue'}">${item.name}</span>
</c:forEach>
</body>
</html>
index.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="${pageContext.request.contextPath}/servlet/DataDao">查看全部</a>
<a href="${pageContext.request.contextPath}/servlet/DataDao?category=1">看java</a>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>StaticFilter</filter-name>
<filter-class>com.filter.StaticFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>StaticFilter</filter-name>
<url-pattern>/servlet/DataDao</url-pattern>
</filter-mapping>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>DataDao</servlet-name>
<servlet-class>com.servlet.DataDao</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DataDao</servlet-name>
<url-pattern>/servlet/DataDao</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>