永发信息网

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

答案:6  悬赏:80  手机版
解决时间 2021-02-25 20:30
  • 提问者网友:伴风望海
  • 2021-02-25 13:07
如何根据数据库字段动态的生成一个页面(html/jsp)
最佳答案
  • 五星知识达人网友:由着我着迷
  • 2021-02-25 13:36
根据数据库字段动态的生成一个页面,利用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楼网友:英雄的欲望
  • 2021-02-25 15:37
这个应该不难吧,你在数据库查询出来,循环显示在html就可以啦。
  • 2楼网友:孤独的牧羊人
  • 2021-02-25 15:31
查询出数据 建一个空白的网页 显示在table 里面 循环出 table 就行了 那么数据就能显示出来
  • 3楼网友:醉吻情书
  • 2021-02-25 15:11
可以使用传参数的方法啊,select*from table1 where +"参数"+;也可以根据判断要使用哪个语句;
如果帮助到您,请记得采纳为满意答案哈,谢谢!祝您生活愉快! vae.la
  • 4楼网友:轻熟杀无赦
  • 2021-02-25 14:11
生成HTML原理
1:制作一个模版

//这是标签

PHP或JSP读取模版内容获取标签,把当前的标签替换成需要输出的数据库内容就可以了
前提是,需要获取之间的标签符号
  • 5楼网友:持酒劝斜阳
  • 2021-02-25 13:42
利用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;
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯