(1)编写程序实现写留言板内容的功能。
编写一个JSP页面writefile.jsp,该页面提供一个表单,用户可以通过表单输入留言标题和内容,提交留言跳转到WriteFileContent.jsp页面。WriteFileContent.jsp页面调用write_file.tag文件,把留言板信息写入C:/Students/save.txt文件中。
write_file.tag文件功能:读出C:/Students/save.txt文件内容,与所提交留言合并,写入该文件中。
(2)编写程序实现读取留言板内容。
编写一个JSP页面readfile.jsp,调用read_file.tag文件,读出C:/Students/save.txt文件内容。
writefile.jsp(网页设计)
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="guan" %>
<form action="../Writefilecontent.jsp" method=get name=form>
<h2>留言板</h2>
<p>留言者:<input type="text" name="theme" /><br>
内容:<textarea name="messa" rows="10" cols="36" ></textarea><br><br>
<input type="submit" value="提交" name="sub" />
</form></body></html>
WriteFileContent.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@taglib tagdir="/WEB-INF/tags" prefix="guan" %>
<body >
<form action="" method=get name=form>
<% String bt=request.getParameter("theme");
String str=request.getParameter("messa");
if(bt==null) bt="";
if(str==null) str="";
byte b[]=bt.getBytes("iso-8859-1");
byte a[]=str.getBytes("iso-8859-1"); //汉字处理
bt=new String(b);
str=new String(a);
%>
<BR><guan:Write_file bt="<%=bt %>" nr="<%=str %>"/> <br>
<%=hnr %>
<a href="../Readfile.jsp">查看留言 </a><br>
<a href="..//Writefile.jsp"> 返回继续留言</a>
</form></body></html>
write_file.tag(需要手动在C盘新建save.txt)
<%@tag pageEncoding="gb2312" %>
<%@tag import="java.io.*" %>
<%@attribute name="nr" required="true" %>
<%@attribute name="bt" required="true" %>
<%@variable name-given="hnr" scope="AT_END" %>
<% File dir=new File("C:/","Students");
File f=new File(dir,"save.txt");
StringBuffer mess=new StringBuffer();
String str;
try{
FileReader in=new FileReader(f);
BufferedReader bufferin=new BufferedReader(in);
String temp;
while((temp=bufferin.readLine())!=null){
mess.append(temp);}
bufferin.close(); //读数据
in.close();}
catch(IOException e){
jspContext.setAttribute("nr",mess);
jspContext.setAttribute("bt",mess);
}
try{
FileWriter outfile=new FileWriter(f);
BufferedWriter bufferout=new BufferedWriter(outfile);
bufferout.write(mess+"#"+bt+":"+nr);
bufferout.close();
outfile.close(); //写数据
jspContext.setAttribute("hnr","留言提交成功");
}
catch(IOException e){
jspContext.setAttribute("hnr","留言提交失败");
}
%>
read_file.tag
<%@tag pageEncoding="gb2312" %>
<%@tag import="java.io.*" %>
<%@tag import="java.util.ArrayList" %>
<%@tag import="java.util.List" %>
<%@attribute name="dir" required="true" %>
<%@attribute name="fileName" required="true" %>
<%@variable name-given="result"
variable-class="java.util.ArrayList" scope="AT_END" %>
<%!
public String readContent(File f)
{ StringBuffer str=new StringBuffer();
try{ FileReader in=new FileReader(f); //读出txt中的数据
BufferedReader bufferin=new BufferedReader(in);
String temp;
while((temp=bufferin.readLine())!=null){
str.append(temp);}
bufferin.close();
in.close();}
catch(IOException e){}
return new String(str); }
%>
<% File f=new File(dir,fileName);
String fileContent=readContent(f);
ArrayList<String> list=new ArrayList<String>();
//返回给JSP页面的list对象
String regex="#";
String words[]=fileContent.split(regex);
for(int i=0;i<words.length;i++){
list.add(words[i]);
}
jspContext.setAttribute("result",list);
%>
readfile.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="guan" %>
<html><body >
<guan:Read_file dir="C:/Students" fileName="save.txt"/>
留言内容:<br>
<%
for(int i=0;i<result.size();i++){
out.print(result.get(i)+"<hr height=10 color=red width=360 />");
}
%>
</form>
</body>
</html>
运行界面