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
    评论
一、设计目的 留言系统目前在各类网站中被广泛使用。通过综合运用JSP、Servlet、JavaBean、JDBC、MySQL和MVC + DAO等技术与方法,设计与实现一个留言管理系统,进一步理解和掌握Java Web编程开发的基本技术和方法,熟练使用开发工具。 二、课程设计内容 设计留言管理系统的目的是提供对留言的增加、删除、修改和显示的功能。 系统分前台和后台两个模块。 1)前台留言模块主要针对普通用户,按功能又分为以下模块。 (1) 发表留言(即添加留言) 普通用户可以在留言管理系统中随意留言,如果留言的字数太少如低于10个字符,系统将给出提示。 (2) 查看留言 可以浏览留言(即查看留言列表)、查看详细留言(即查看某条留言内容)、搜索留言(即查看符合条件的留言列表)、查看友情链接(即导航到别的网站)。 2)后台模块(管理模块)针对管理员提供操作,按功能又分为以下模块。 (1) 登录管理:需要输入管理员账号、口令和验证码,系统将验证、口令和验证码是否正确,如果验证成功,则进入管理员界面;否则,系统提示账号或密码错误的信息。 另外,登录后的管理员可以选择退出登录,进而退出管理界面。(实现提示:将退出请求提交给处理退出的Servlet,在该Servlet中将session失效,再将页面重定向到登录页面。) (2) 留言管理:可以查看所有的留言,可以增加留言(即回复留言)、修改留言、删除留言
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

悢七

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

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

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

打赏作者

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

抵扣说明:

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

余额充值