jsp编写程序实现写读留言板内容的功能

(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>

运行界面

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

  • 5
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悢七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值