在jsp中進行檔案的讀寫操作
1.實驗目的
本實驗的目的是掌握怎樣在JSP中進行檔案的讀寫操作。
2.實驗內容
編寫三個JSP頁面giveContent.jsp,writeContent.jsp、readContent.jsp以及兩個Tag檔案WriteTag.tag和ReadTag.jsp。
1.giveContent.jsp的具體要求
giveConten.jsp頁面提供一個表單,要求該表單提供一個text文字輸入框、select下拉列表和一個TextArea文字區,使用者可以在text輸入框輸入檔案的名字、在select下拉列表選擇一個目錄(下拉列表的選項必須是Tomcat伺服器所駐留計算機上的目錄)、通過TextArea輸入多行文字。單擊表單的提交鍵將text中輸入的檔名字、select下拉列表中選中的目錄以及TextArea文字區中的內容提交給writeContent.jsp頁面。
2.writeContent.jsp的具體要求
writeContent.jsp頁面首先獲得giveConten.jsp頁面提交的檔案所在目錄、名字以及TextArea文字區中的內容,然後使用Tag標記呼叫Tag檔案WriteTag.tag,並將檔案所在目錄、名字以及TextArea文字區中的內容傳遞給WriteTag.tag。
3.lookContent.jsp的具體要求
lookContent.jsp頁面提供一個表單,該表單提供兩個text文字輸入框,使用者可以這兩個text文字輸入框輸入目錄和檔名字。單擊表單的提交鍵將text中輸入的檔案目錄以及檔名字提交給readContent頁面。
4.readContent.jsp的具體要求
readContent.jsp頁面首先獲得lookConten.jsp頁面提交的檔案目錄、名字,然後使用Tag標記呼叫Tag檔案ReadTag.tag,並將檔案所在目錄、名字傳遞給ReadTag.tag。
5.WriteTag.tag的具體要求
WriteTag.tag檔案使用attribute指令獲得writeContent.jsp頁面傳遞過來的檔案目錄、檔名字和檔案內容,然後使用I/O流將檔案內容寫入到檔案中,該檔案所在目錄就是writeContent.jsp頁面傳遞過來的檔案目錄,名字就是writeContent.jsp頁面傳遞過來的檔名字。
6.ReadTag.tag的具體要求
Read.tag檔案使用attribute指令獲得readContent.jsp頁面傳遞過來的檔案目錄和檔名字,然後使用I/O流讀取檔案,並負責顯示所讀取的內容。
1.2 實現程式碼
1,giveContent.jsp
<%@ page contentType="text/html;charset=utf-8" %>
<HTML><Body bgcolor=yellow>
<HEAD>
<jsp:include page="head2.txt" />
</HEAD>
<FORM action="writeContent.jsp" method="post" >
<br>請選擇一個檔案目錄:
<select name="directory">
<option value=D:\學software\jsp\WebContent>D:\學software\jsp\WebContent
<option value=D:\TomCat\apache-tomcat-8.5.20\webapps\MyJsp>D:\TomCat\apache-tomcat-8.5.20\webapps\MyJsp
<option value=D:\jsp檔案>D:\jsp檔案
</select><br>
<br>輸入儲存檔案的名字:
<input type="text" name="fileName" ><br>
<br>輸入檔案的內容:
<br><TextArea name="content" rows="7" cols="40"></TextArea>
<br><br><input type="submit" name="submit" value="提交">
</FORM>
</Body></HTML>
2,writeContent.jsp
<%@ page contentType="text/html;charset=utf-8" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="writefile" %>
<HTML><Body bgcolor=cyan>
<%
request.setCharacterEncoding("utf-8");
String Dir=request.getParameter("directory");
String FName=request.getParameter("fileName");
String Con=request.getParameter("content");
%>
<writefile:WriteTag dir="<%=Dir%>" fname="<%=FName%>" con="<%=Con%>"/>
<%
if(result)
{
out.println("檔案寫入成功!");
out.println("<br>"+"檔案所在目錄:"+Dir);
out.println("<br>檔案的名字:"+FName);
}
else
{
out.println("檔案寫入失敗!");
}
%>
</Body>
</HTML>
3,WriteTag.tag
<%@ tag import="java.io.*" %>
<%@ tag pageEncoding="utf-8" %>
<%@ attribute name="dir" required="true" %>
<%@ attribute name="fname" required="true" %>
<%@ attribute name="con" required="true" %>
<%@ variable name-given="result" variable-class="java.lang.Boolean" scope="AT_END" %>
<%!
public void writeContent(String str,File f)
{
try
{
FileWriter outfile=new FileWriter(f);
BufferedWriter bufferout=new BufferedWriter(outfile);
bufferout.write(str);
bufferout.close();
outfile.close();
}
catch(IOException e)
{
}
}
%>
<%
boolean flag=false;
File f=new File(dir,fname);
/*
out.println("dir"+dir);
out.println("fname"+fname);
out.println("con"+con);
*/
/*
FileOutputStream op=new FileOutputStream(f);
BufferedOutputStream bufferout=new BufferedOutputStream(op);
byte b[]=con.getBytes();
bufferout.write(b);
bufferout.flush();
bufferout.close();
op.close();
*/
if(con.length()>0)
{
writeContent(con,f);
}
flag=true;
jspContext.setAttribute("result",new Boolean(flag));
%>
4,lookContent.jap
<%@ page contentType="text/html;charset=utf-8" %>
<html><body bgcolor=yellow>
<HEAD>
<jsp:include page="head2.txt"/>
</HEAD>
<form action="readContent.jsp" method=post >
輸入檔案的路徑(如:d/1000):
<input type="text" name="path"><br>
輸入檔案的名字:
<input type="text" name="name"><br>
<input type="submit" name="submit" value="讀取">
</form>
</body></html>
5,readContent.jsp
<%@ page contentType="text/html;charset=utf-8" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="read" %>
<html><body bgcolor=cyan>
<%
request.setCharacterEncoding("utf-8");
String filePath=request.getParameter("path");
String fileName=request.getParameter("name");
%>
<read:ReadTag fdir="<%=filePath%>" fname="<%=fileName%>"/>
</body></html>
6,ReadTag.tag
<%@ tag import="java.io.*" %>
<%@ tag pageEncoding="GB2312" %>
<%@ attribute name="fdir" required="true" %>
<%@ attribute name="fname" required="true" %>
<%!
public String readContent(File f)
{
StringBuffer str=new StringBuffer();
try
{
FileReader in=new FileReader(f);
BufferedReader bufferin=new BufferedReader(in);
String temp;
while((temp=bufferin.readLine())!=null)
{
str.append(temp);
str.append("<br>");
}
bufferin.close();
in.close();
}
catch(IOException e)
{ }
return new String(str);
}
%>
<%
File f=new File(fdir,fname);
String str=readContent(f);
out.println(f.getName()+"的內容:<br>");
out.print(str);
%>
執行結果:
若出現中文亂碼:
在每個jsp頁面中的request.getParemeter(“String”);前加入request.setCharacterEncoding(“utf-8”);即可