JSP动态读取txt文件问题,求指导 应客户要求,本身很简单的问题。现在有点棘手了。功能简单说一下。类似各大网站注册时候或者第一次进入大型游戏的时候,会有条约同意画面。现在这个画面里面的内容是以txt保存的。我需要做的是读取数据库里的这个txt文件的路径之后,然后返回到jsp页面动态的读取这个txt文件的内容。现在问题就是,因为是动态的,我一开始用textarea+include写死了就用不了了。现在路径已读取出来,不知道如何在页面上获取这个路径并展示出来。求指教。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><head><body> <iframe src="" id="id" frameborder="1" height="100" width="200"></iframe> <script type="text/javascript"> function aa(){ var ss = document.getElementById("id"); ss.src ="${url}";//用el表达式动态加载url } window.load = aa(); </script><body></html> 可以使用ssi 或者 FileInputStreampublic static String readFile() throws IOException, FileNotFoundException { File textFile = new File(textFilePath); if(!textFile.exists()){ throw new FileNotFoundException(textFilePath + "文件不存在"); } InputStream in = new FileInputStream(textFile); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuffer buf = new StringBuffer(textFile.length()/2); String line = null; while((line = in.readLine()) != null){ buf.append(line).append("\n"); } reader.close(); in.close(); return buf.toString();} 我就觉得先用一段程序读出txt中的数据,可以保存成String了,然后再将该String传递到jsp页面中了,在输出来。当然txt中的类容就可以是动态的了。 是好招, 但是要注意路径建议在服务器端读出打印出来, 如果需要减少http请求数的话 是我的话我就每次先读出内容再传给jsp 用ajax操作,要是你的txt是有规律的可以用 一个xml格式将数据传给前台jsp页面,之后通过ajax将xml解析,同时拼装成一个jsp插入到你的目的div中。。即可 weblogic部署项目问题 session过时问题 有关java.lang.NullPointerException错误的问题 tomcat + jsp 有没有方法让网址不区分大小写? DWR的问题,为什么我在编译JAVA文件后,html页面中js的方法没有改变?救命啊!!! 急急急!!大家可不可以帮一下,快来不及了。。hibernate 自己写SQL 文 怎么在JSP页面内执行装在字符串内的JS代码 刚才的问题已结,在问另个低级问题 在win2000下如何用jdk编译和运行java程序? tomcat的问题。 求助,如何在JS图片切换中加入特殊效果? 问一个关于不同角色使用不同菜单的问题
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><head><body>
<iframe src="" id="id" frameborder="1" height="100" width="200"></iframe>
<script type="text/javascript">
function aa(){
var ss = document.getElementById("id");
ss.src ="${url}";//用el表达式动态加载url
}
window.load = aa();
</script>
<body>
</html>
public static String readFile() throws IOException, FileNotFoundException {
File textFile = new File(textFilePath);
if(!textFile.exists()){
throw new FileNotFoundException(textFilePath + "文件不存在");
}
InputStream in = new FileInputStream(textFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer buf = new StringBuffer(textFile.length()/2);
String line = null;
while((line = in.readLine()) != null){
buf.append(line).append("\n");
}
reader.close();
in.close();
return buf.toString();
}
是好招, 但是要注意路径建议在服务器端读出打印出来, 如果需要减少http请求数的话