最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java高并发生成静态页面生成方案
时间:2012-06-12 编辑:简简单单 来源:一聚教程网
提升网站性能的方式有很多,例如有效的使用缓存,生成静态页面等等。今天要说的就是生成静态页面的方式。这个也是我近期一直在搞的一个问题,近期在做使用html + servlet做个人网站,为什么是这2个东西呢?
其中,对于 URL Rewriter的部分,可以使用收费或者开源的工具来实现,如果 url不是特别的复杂,可以考虑在 servlet 中实现,那么就是下面这个样子:
javascript中XMLDOM和parseXML解析xml实现代码
js xml,xmldom解析xml
本文章来介绍了关于javascript中XMLDOM和parseXML解析xml实现代码,有需要的同学可看看用法与参考方法。
现在我们来看看一个普通的url分页请求怎么生成静态页面。
假如现在要显示第一页的数据信息 ,那么通常的链接会是这个样子 : http://abc.com/xx.do?pageNumber=1 。我们说一下这个最简单的方式,打个比方:当我们访问 http://abc.com/xx_pageNumber_1.shtml 的时候 ,就是在访问 http://abc.com/xx.do?pageNumber=1 。规律就是请求的 action(动作),和参数的名称,参数的值,都用下滑线分开,而且请求的网页的最终后缀是 .shtm 。然后呢?我们需要编写一个servlet,去处理 .shtm 的请求:
代码如下
复制代码
public
class
CreatorHtmlServlet
extends
HttpServlet {
private
Logger log=Logger.getLogger(CreatorHtmlServlet.
class
);
public
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
doPost(request, response);
}
public
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
String encoding =
"UTF-8"
;
//获得请求address
String templatePath = URLReWrite(request);
//获得相对路径
String realPath= request.getSession().getServletContext().getRealPath(
"/"
);
//获得要生成的静态html文件的名字
String htmlName = getHtmlFileName(request);
//获得详细路径的 html文件的名字
String cachhtmlFileName = realPath + File.separator + htmlName;
File cacheFile =
new
File(cachhtmlFileName);
boolean
load =
true
;
//文件存在的话就可以直接返回了,不需要做任何处理
if
(cacheFile.exists()) {
load =
false
;
}
if
(load) {
final
ServletOutputStream stream =
new
ServletOutputStream() {
public
void
write(
byte
[] data,
int
offset,
int
length) {
os.write(data, offset, length);
}
public
void
write(
int
b)
throws
IOException {
os.write(b);
}
};
final
PrintWriter pw =
new
PrintWriter(
new
OutputStreamWriter(os, encoding));
HttpServletResponse rep =
new
HttpServletResponseWrapper(response) {
public
ServletOutputStream getOutputStream() {
return
stream;
}
public
PrintWriter getWriter() {
return
pw;
}
};
logger.debug(
"HtmlCreatorServlet RequestDispatcher = "
+ templatePath);
// 使用 RequestDispatcher转发请求,请求真是的地址
// 例如 index.shtm ,则转发到 index.do
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(templatePath);
dispatcher.include(request, rep);
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
);
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;
}
我们这章要讨论一下如何让用户遵守访问方式和如何更新生成的html文件-
上一个: Java中Json数据的测试代码
相关文章
- 生成静态页面 04-24
- 通过模板生成静态页面示例 01-18
- 谈谈生成静态页面的一些经验 01-11
- SpringBoot测试配置属性与web启动环境解析 10-24
- vue中将el-switch值true、false改为number类型的1和0解析 10-24
- Vue中的路由配置项meta使用解读 10-24