java高并发-静态页面生成方案(2)
2012-06-06 07:45:31 来源:WEB开发网核心提示:pw.flush();FileOutputStream fos = null;try { if(os.size() == 0) {// 验证一下用户转发的地址是否有效,无效的话就提示错误response.sendError(HttpServletResponse.SC_NOT_FOUND, "");
pw.flush();
FileOutputStream fos = null;
try {
if(os.size() == 0) {
// 验证一下用户转发的地址是否有效,无效的话就提示错误
response.sendError(HttpServletResponse.SC_NOT_FOUND, "");
}
else {
// servlet调用其他命令在相应的目录生成html文件,并且把文件返回给客户端
fos = new FileOutputStream(cachhtmlFileName);
os.writeTo(fos);
dispatcher = getServletContext(). getRequestDispatcher("/"+htmlName);
dispatcher.include(request, response);
}
} finally {
if(fos != null) {
fos.close();
}
}
} else {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/"+htmlName); dispatcher.include(request, response);
}
}
// 主要的功能就是把http://abc.com/xx_pageNumber_1.shtm
// 转换成 http://abc.com/xx.do?pageNumber=1 形式
protected String URLReWrite(HttpServletRequest request) throws ServletException, IOException {
String uri = request.getRequestURI();
String contextPath = request.getContextPath();
logger.debug("HtmlCreator contextPath = " + contextPath);
if (contextPath != null && contextPath.length() > 0)
uri = uri.substring(contextPath.length());
uri = uri.substring(0, uri.length()-5);
String[] urls = uri.split("_");
uri = urls[0] + ".do";
if(urls.length > 1) {
for(int i = 1; i < urls.length; i += 2) {
if(i==1) {
uri += "?" + urls[i] + "=" + urls[i+1];
} else {
uri += "&" + urls[i] + "=" + urls[i+1];
}
}
}
logger.debug("HtmlCreatorServlet get uri = " + uri);
return uri;
}
// 主要功能根据 http://abc.com/xx_pageNumber_1.shtm
// 来得到即将要生成的html文件名字,也就是 xx_pageNumber_1.html
private String getHtmlFileName(HttpServletRequest request) throws ServletException, IOException{ String uri = request.getRequestURI();
String contextPath = request.getContextPath();
if (contextPath != null && contextPath.length() > 0)
uri = uri.substring(contextPath.length());
uri = uri.substring(1, uri.length()-5);
uri += ".html";
return uri;
}
上面就是整个代码了,非常的简单吧
FileOutputStream fos = null;
try {
if(os.size() == 0) {
// 验证一下用户转发的地址是否有效,无效的话就提示错误
response.sendError(HttpServletResponse.SC_NOT_FOUND, "");
}
else {
// servlet调用其他命令在相应的目录生成html文件,并且把文件返回给客户端
fos = new FileOutputStream(cachhtmlFileName);
os.writeTo(fos);
dispatcher = getServletContext(). getRequestDispatcher("/"+htmlName);
dispatcher.include(request, response);
}
} finally {
if(fos != null) {
fos.close();
}
}
} else {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/"+htmlName); dispatcher.include(request, response);
}
}
// 主要的功能就是把http://abc.com/xx_pageNumber_1.shtm
// 转换成 http://abc.com/xx.do?pageNumber=1 形式
protected String URLReWrite(HttpServletRequest request) throws ServletException, IOException {
String uri = request.getRequestURI();
String contextPath = request.getContextPath();
logger.debug("HtmlCreator contextPath = " + contextPath);
if (contextPath != null && contextPath.length() > 0)
uri = uri.substring(contextPath.length());
uri = uri.substring(0, uri.length()-5);
String[] urls = uri.split("_");
uri = urls[0] + ".do";
if(urls.length > 1) {
for(int i = 1; i < urls.length; i += 2) {
if(i==1) {
uri += "?" + urls[i] + "=" + urls[i+1];
} else {
uri += "&" + urls[i] + "=" + urls[i+1];
}
}
}
logger.debug("HtmlCreatorServlet get uri = " + uri);
return uri;
}
// 主要功能根据 http://abc.com/xx_pageNumber_1.shtm
// 来得到即将要生成的html文件名字,也就是 xx_pageNumber_1.html
private String getHtmlFileName(HttpServletRequest request) throws ServletException, IOException{ String uri = request.getRequestURI();
String contextPath = request.getContextPath();
if (contextPath != null && contextPath.length() > 0)
uri = uri.substring(contextPath.length());
uri = uri.substring(1, uri.length()-5);
uri += ".html";
return uri;
}
上面就是整个代码了,非常的简单吧
更多精彩
赞助商链接