如何根据数据库字段动态的生成一个页面?

如何根据数据库字段动态的生成一个页面?,第1张

根据数据库字段动态的生成一个页面,利用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!= -1i = 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

}

}

这个你要做一个列表页和一个详细

列表页显示的是你根据条件查询出来的商品信息

关键是详细页

在列表页点击商品后可以获取到商品的ID值(用比如gridview之类的控件)然后通过传值

传到详细页

那么知道id了

在根据id读取不就得了么


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9670162.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存