我想做个JSP一个发布信息的,我想把信息插入到数据库里,然后在别的页面显示出来,但是显示的时候没有了格式,就是没有了换行、空格和缩进,我问别人,他们说在插入的时候也应该把HTML也插入进去,请问我该怎么做呢?或者大家有没有别的更好更简便的方法呢???
谢谢进来的各位们!
谢谢进来的各位们!
解决方案 »
- 关于DiskFileUpload上传文件的问题!
- 救命啊,顶者有分
- 有人做过统计么?现在流行web框架的使用率
- 关于环境变量设置classpath
- SERVLET基础连接SQL问题
- 请教:在采用 Struts + Hibernate 框架的情况下,如何在JSP中显示MySQL数据库中BLOB类型的图片(详细见内文),多谢!
- 一个jsp+tomcat连接mysql的问题,请大家帮助看看!!!!!1
- 模拟登录网页的代码。谢谢啊。(协议是https)
- jsp调用的htm页面中文乱码?
- 关于BigDecimal方法,大的double数相减
- 寻求禁止提取网页内容的好方法
- 请高手帮我写一条SQL语句,符合实现的有奖励100分
将原来的文章管理系统由JSP文件的从数据库中取数据显示改为由jsp生成静态html文件后直接访问html文件。下面是一个简单的示例。.buildhtml.jsp
<%@ page contentType="text/html; charset=gb2312" import="java.util.*,java.io.*"%>
<%
try{
String title="This is Title";
String content="This is Content Area";
String editer="LaoMao";
String filePath = "";
filePath = request.getRealPath("/")+"test/template.htm";
//out.print(filePath+"<br/>");
String templateContent="";
FileInputStream fileinputstream = new FileInputStream(filePath);//读取模块文件
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);
//out.print(templateContent);
templateContent=templateContent.replaceAll("###title###",title);
templateContent=templateContent.replaceAll("###content###",content);
templateContent=templateContent.replaceAll("###author###",editer);//替换掉模块中相应的地方
//out.print(templateContent);
// 根据时间得文件名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
fileame = request.getRealPath("/")+fileame;//生成的html文件保存路径
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}
catch(Exception e){
out.print(e.toString());
}
%>
2. template.htm
<html>
<head>
<title>###title###</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="../css.css" rel=stylesheet type=text/css>
</head>
<body>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="2">
<tr>
<td align="center">###title###</td>
</tr>
<tr>
<td align="center">author:###author### </td>
</tr>
<tr>
<td>###content###
</td>
</tr>
</table>
</body>
</html>
我先去试试。
<%
String str = ""; // 你从数据库拿到的内容
str = str.replace("\r\n","<br/>");
str = str.replace("\n","<br/>");
str = str.replace(" "," ");
out.print(str);
%>
那么他生成的静态htm文件会不会还保留?还是过一段时间自动删除了?
你那方式我用过了
现在还有个问题 就是我在主页上显示新闻的时候应该怎么样获取他的新闻题目?
想了半天,没想出来,请赐教!
<c:out value="${content}" />
</pre>
或者
<textarea style="border:0px"><c:out value="${content}" /></textarea>
难不成你在后台录入新闻资料时不录标题?!
那又怎么录入标题?既然要数据库,那又何必要用这种方法???