<html>
<head>
<title>JSP Form</title>
<style>
</style>
</head>
<body>
<form action="TestFileHandling.jsp" method="post">
<fieldset>
<legend>User Information</legend>
<label for="question">Question</label>
<input type="text" name="question" /> <br/>
<input type="submit" value="submit">
</fieldset>
</form>
</body>
</html>
以上是讓用戶在發送問題之前輸入問題的簡單表單。JSP中的表格 - 在每篇文章旁邊添加日期
<%@page import="myPackage.FileReaderWriter"%>
<%@page import="java.util.Vector"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Vector<String[]> v = new Vector<String[]>();
String[] str1 = {request.getParameter("question")};
v.addElement(str1);
FileReaderWriter.saveVectorToFile(v, "MyTestFile.txt");
%>
<%
Vector<String[]> vec = FileReaderWriter.readFileToVector ("MyTestFile.txt");
for (int i = 0; i < vec.size(); i++)
{
out.print("|");
for (int j = 0; j < vec.elementAt(i).length; j++)
{
out.print(vec.elementAt(i)[j] + "|");
}
%>
<br>
<%
}
%>
</body>
</html>
這部分將輸入的問題保存到一個文本文件中,然後打開文件以顯示裏面的內容。
所有這一切都是通過下面的Java代碼來完成:
package myPackage;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;
public class FileReaderWriter {
public static void saveVectorToFile(Vector<String[]> v, String sFileName)
{
try
{
// Create a new file writer
FileWriter writer = new FileWriter(sFileName, true);
// Loop through all the elements of the vector
for (int i = 0; i < v.size(); i++)
{
// Capture the index of the last item of each array
int lastIndex = v.elementAt(i).length - 1;
// Loop through all the items of the array, except
// the last one.
for (int j = 0; j < lastIndex; j++)
{
// Append the item to the file.
writer.append(v.elementAt(i)[j]);
// Append a comma after each item.
writer.append(',');
}
// Append the last item.
writer.append(v.elementAt(i)[lastIndex]);
// Append a new line character to the end of the line
// (i.e. Start new line)
writer.append('\n');
}
// Save and close the file
writer.flush();
writer.close();
}
// Catch the exception if an Input/Output error occurs
catch (IOException e)
{
e.printStackTrace();
}
}
public static Vector<String[]> readFileToVector(String sFileName)
{
// Initialise the BufferedReader
BufferedReader br = null;
// Create a new Vector. The elements of this Vector are String arrays.
Vector<String[]> v = new Vector<String[]>();
try
{
// Try to read the file into the buffer
br = new BufferedReader(new FileReader(sFileName));
// Initialise a String to save the read line.
String line = null;
// Loop to read all the lines
while ((line = br.readLine()) != null)
{
// Convert the each line into an array of Strings using
// comma as a separator
String[] values = line.split(",");
// Add the String array into the Vector
v.addElement(values);
}
}
// Catch the exception if the file does not exist
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
// Catch the exception if an Input/Output error occurs
catch (IOException ex)
{
ex.printStackTrace();
}
// Close the buffer handler
finally
{
try
{
if (br != null)
br.close();
} catch (IOException ex)
{
ex.printStackTrace();
}
}
// return the Vector
return v;
}
}
的部分這就是真正的困惑我的是我怎麼會修改這個讓後一個問題張貼形式的日期和時間帖子會自動添加到每個問題的開頭。
要做到這一點,我知道我將首先需要進口之日起效用的Java,然後把這樣的事情我窗體頁上:
<%!Date startTime = new Date();%>
一旦我得到的那部分我開始思考自己,我如何將startTime中的信息傳遞到處理向量附加的java文件中?
別的東西,我試過已被簡單地把Date startTime = new Date();
到Java文件,然後使用一個簡單的代碼一樣writer.append(startTime);
讓裏面startTime
日期與進入的問題一起追加,然而這並沒有在所有的工作,只是給了我錯了。最後,這使我相信,要做到這一點,最好的辦法是隻使用scriplet:
<%!Date startTime = new Date();%>
我究竟將如何傳遞startTime
扶住我的Java代碼的信息,以便它可以在裏面附輸入問題的向量的相同元素被保存到?感謝您的任何幫助或建議。
編輯:有人也請解釋爲什麼writer.append(startTime);
不起作用?現在看來似乎應該完全正常工作...希望瞭解什麼是錯會帶給我更近了一步搞清楚了這一點
Ah很好,謝謝,是的,我一直有很多人告訴我不要這樣編碼,但不幸的是它是我的uni想要的。 – JimmyK 2012-02-15 15:58:54