如何向数据库中存储html数据

如何向数据库中存储html数据,第1张

1.

数据库中保存的html内容都是文本格式,

数据库字段如oracle选用varchar2(2000)或clob(大对象)

2.

一般的html编辑器,是对上传的对象(如图片、文档等)保存到服务器临时目录,并在html代码中保存html连接的方式

因此,向数据库中写html页面实际上就相当于把html的文本保存到数据库中,如果文本较大,

可以用中间变量做缓存(buffer)

利用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

}

}

数据库字段是放在表里的,字段就像是一个标识,一个表里可以有多个字段,在程序设计里要用到数据库的话,就是靠这个标识来读写数据库里面的内容的。例如

要在数据库里存名字的话,那就写个字段为name

把名字放在对应的字段就可以了!

不是很懂你要问的问题!

如果你是想要问sql语句的代码的话,就是楼上说的那些,你可以去找一本数据库的书看看就可以了!


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

原文地址:https://54852.com/sjk/6685310.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存