如何根据数据库字段动态的生成一个页面(html/jsp)

如何根据数据库字段动态的生成一个页面(html/jsp)页面 根据数据库的字段来决定比如数据库字段是 id name 那么页面就是 一个Id 标签 和一个输入框 一个name标签 和一个name输入框 如果问题解决,另外追分。。。期待高手。

利用Filter和定制Response,把服务器返回的JSP响应输出到我们自己的Response中,就可以将响应快速写入Html文件,然后再发送给客户。

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Calendar;

public class CacheFilter implements Filter {
ServletContext sc;
FilterConfig fc;
long cacheTimeout = Long.MAX_VALUE;

public void doFilter(ServletRequest req,
ServletResponse res,
FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request =
(HttpServletRequest) req;
HttpServletResponse response =
(HttpServletResponse) res;

// check if was a resource that shouldn't be cached.
String r = sc.getRealPath("");
String path =
fc.getInitParameter(request.getRequestURI());
if (path!= null && path.equals("nocache")) {
chain.doFilter(request, response);
return;
}
path = r+path;

String id = request.getRequestURI() +
request.getQueryString();
File tempDir = (File)sc.getAttribute(
"javax.servlet.context.tempdir");

// get possible cache
String temp = tempDir.getAbsolutePath();
File file = new File(temp+id);

// get current resource
if (path == null) {
path = sc.getRealPath(request.getRequestURI());
}
File current = new File(path);

try {
long now =
Calendar.getInstance().getTimeInMillis();
//set timestamp check
if (!file.exists() || (file.exists() &&
current.lastModified() > file.lastModified()) ||
cacheTimeout < now - file.lastModified()) {
String name = file.getAbsolutePath();
name =
name.substring(0,name.lastIndexOf("/"));
new File(name).mkdirs();
ByteArrayOutputStream baos =
new ByteArrayOutputStream();
CacheResponseWrapper wrappedResponse =
new CacheResponseWrapper(response, baos);
chain.doFilter(req, wrappedResponse);

FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
}
} catch (ServletException e) {
if (!file.exists()) {
throw new ServletException(e);
}
}
catch (IOException e) {
if (!file.exists()) {
throw e;
}
}

FileInputStream fis = new FileInputStream(file);
String mt = sc.getMimeType(request.getRequestURI());
response.setContentType(mt);
ServletOutputStream sos = res.getOutputStream();
for (int i = fis.read(); i!= -1; i = fis.read()) {
sos.write((byte)i);
}
}

public void init(FilterConfig filterConfig) {
this.fc = filterConfig;
String ct =
fc.getInitParameter("cacheTimeout");
if (ct != null) {
cacheTimeout = 60*1000*Long.parseLong(ct);
}
this.sc = filterConfig.getServletContext();
}

public void destroy() {
this.sc = null;
this.fc = null;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-04-16

根据数据库字段动态的生成一个页面,利用Filter和定制Response,把服务器返回的JSP响应输出到我们自己的Response中,就可以将响应快速写入Html文件,然后再发送给客户。 

import java.io.*; 

import javax.servlet.*; 

import javax.servlet.http.*; 

import java.util.Calendar; 

public class CacheFilter implements Filter { 

ServletContext sc; 

FilterConfig fc; 

long cacheTimeout = Long.MAX_VALUE; 

public void doFilter(ServletRequest req, 

ServletResponse res, 

FilterChain chain) 

throws IOException, ServletException { 

HttpServletRequest request = 

(HttpServletRequest) req; 

HttpServletResponse response = 

(HttpServletResponse) res; 

// check if was a resource that shouldn't be cached. 

String r = sc.getRealPath(""); 

String path =  

fc.getInitParameter(request.getRequestURI()); 

if (path!= null && path.equals("nocache")) { 

chain.doFilter(request, response); 

return; 

path = r+path; 

String id = request.getRequestURI() +  

request.getQueryString(); 

File tempDir = (File)sc.getAttribute( 

"javax.servlet.context.tempdir"); 

// get possible cache 

String temp = tempDir.getAbsolutePath(); 

File file = new File(temp+id); 

// get current resource 

if (path == null) { 

path = sc.getRealPath(request.getRequestURI()); 

File current = new File(path); 

try { 

long now = 

Calendar.getInstance().getTimeInMillis(); 

//set timestamp check 

if (!file.exists() || (file.exists() && 

current.lastModified() > file.lastModified()) || 

cacheTimeout < now - file.lastModified()) { 

String name = file.getAbsolutePath(); 

name = 

name.substring(0,name.lastIndexOf("/")); 

new File(name).mkdirs(); 

ByteArrayOutputStream baos = 

new ByteArrayOutputStream(); 

CacheResponseWrapper wrappedResponse = 

new CacheResponseWrapper(response, baos); 

chain.doFilter(req, wrappedResponse); 

FileOutputStream fos = new FileOutputStream(file); 

fos.write(baos.toByteArray()); 

fos.flush(); 

fos.close(); 

} catch (ServletException e) { 

if (!file.exists()) { 

throw new ServletException(e); 

catch (IOException e) { 

if (!file.exists()) { 

throw e; 

FileInputStream fis = new FileInputStream(file); 

String mt = sc.getMimeType(request.getRequestURI()); 

response.setContentType(mt); 

ServletOutputStream sos = res.getOutputStream(); 

for (int i = fis.read(); i!= -1; i = fis.read()) { 

sos.write((byte)i); 

public void init(FilterConfig filterConfig) { 

this.fc = filterConfig; 

String ct = 

fc.getInitParameter("cacheTimeout"); 

if (ct != null) { 

cacheTimeout = 60*1000*Long.parseLong(ct); 

this.sc = filterConfig.getServletContext(); 

public void destroy() { 

this.sc = null; 

this.fc = null; 

}

本回答被网友采纳
第2个回答  2013-07-18
生成HTML原理
1:制作一个模版
<html>
<!--{id}--> //这是标签

</html>
PHP或JSP读取模版内容获取标签,把当前的标签替换成需要输出的数据库内容就可以了
前提是,需要获取<!--{ }-->之间的标签符号
第3个回答  2013-07-18
可以使用传参数的方法啊,select*from table1 where +"参数"+;也可以根据判断要使用哪个语句;

如果帮助到您,请记得采纳为满意答案哈,谢谢!祝您生活愉快! vae.la
第4个回答  2013-07-18
查询出数据 建一个空白的网页 显示在table 里面 循环出 table 就行了 那么数据就能显示出来