jsp生成静态页面
来源:互联网 发布:隐形弹簧升级数据 编辑:程序博客网 时间:2024/06/09 19:58
使用jsp生成静态页面.整个工程的目录结构如图:
StaticPage是我们将要生成的静态页面目录 所有静态页面都会生成在这个目录.
然后编写一个MyFitler的过滤器:
以下是代码片段:
package com.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.*;
public class MyFilter implements Filter {
private FilterConfig filterConfig;
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
String para = arg0.getParameter("id");
if (para != null) {
// 获得静态文件生成的绝对地址
para = arg0.getRealPath("/") + "StaticPage/" + para + ".html";
File file = new File(para);
// 判断文件是否存在
if (!file.exists()) {// 不存在 生成
FileWriter fileWriter = new FileWriter(file);
StringBuilder sb = new StringBuilder(1000);
sb.append("这个页面是静态页面
" + para + "
");
sb.append("当前时间:" + new java.util.Date().toLocaleString()
+ "
");
sb
.append("<a>back</a>");
fileWriter.write(sb.toString());
fileWriter.flush();
fileWriter.close();
}
// 转向到该html页面
HttpServletResponse hsr = (HttpServletResponse) arg1;
hsr.sendRedirect("/BuildeStatic/StaticPage/"
+ arg0.getParameter("id") + ".html");
return;
}
arg2.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
this.filterConfig = filterConfig;
}
}
然后配置web.xml使上面的过滤器针对StaticPage目录生效:
然后编写StaticPage下的index.jsp文件 作为目录页面
以下是代码片段:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>index</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="/BuildeStatic/StaticPage/index.jsp?id=123">1号静态页面</a>
<br>
<a href="/BuildeStatic/StaticPage/index.jsp?id=124">2号静态页面</a>
<br>
<a href="/BuildeStatic/StaticPage/index.jsp?id=125">3号静态页面</a>
<br>
<a href="/BuildeStatic/StaticPage/index.jsp?id=126">4号静态页面</a>
<br>
</body>
</html>
- Jsp生成静态页面
- jsp生成静态页面
- Jsp生成静态页面
- jsp生成静态页面
- jsp生成静态页面
- JSP生成静态页面
- JSP生成静态页面
- jsp 生成静态页面
- jsp生成静态页面
- jsp生成静态html页面
- jsp动态生成静态页面
- jsp生成html静态页面
- JSP 生成静态HTML页面
- jsp 页面生成静态页面(调包response)
- JSP结合JavaBean生成静态页面示例
- jsp生成静态html页面思路【2】
- jsp网站的静态页面生成方案
- jsp生成静态页面的方法
- Promise推新一代软件架构SATA RAID 5存储保护
- C/C++ young library 设计与实现 — 移植
- GridView根据值的变化改变行列样式
- 中国计算机报:虚实华为3Com
- weblogic7 下应用的部署以及配置
- jsp生成静态页面
- GridView的几个事件(如实现: 行的双击/单击/捕捉键盘按键/鼠标悬浮/移出效果)(示例代码下载)
- c#实现singleton
- GridView的常见问题
- 自然语言处理在搜索引擎信息检索中的应用
- Dos下的一些命令的使用技巧
- 循环赛日程表问题之二
- 2006年11月30日,今天的收获很大---Broadview印象
- [翻译]辉煌帝传说 第三章 生命之树(8)上