JSP 中的表单 - 在每个帖子旁边添加日期
<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>
上面是一个简单的表单,允许用户在发送之前输入问题。
<%@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);
不起作用吗?看起来它应该工作得很好......希望理解出了什么问题能让我更接近解决这个问题
<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>
The above is a simple form that lets the user enter a question before sending it.
<%@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>
This part takes the question entered and saves it to a text file and then opens the file to display whatever is inside.
All this is done through the following java code:
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;
}
}
The part thats really confusing me is how would I edit this so that after a question is posted on the form, the date and time of the post is automatically added to the beginning of each question.
To do this I know I would first need to import the date utility in java and then put something like this on my form page:
<%!Date startTime = new Date();%>
Once I get to that part I start thinking to myself, how would I pass the information inside startTime onto my java file that handles the appending of the vector?
Something else I tried already was simply putting Date startTime = new Date();
into the java file and then using a simple code like writer.append(startTime);
so that the date inside startTime
is appended along with the question entered, however this didn't work at all and just gave me a error. In the end this led me to believe that the best way to do this is to just use the scriplet:
<%!Date startTime = new Date();%>
How exactly would I pass the information held in startTime
onto my java code so that it can be appended inside the same element of the vector that the question entered was saved to? Thanks for any help or advice.
EDIT: can someone also please explain why writer.append(startTime);
doesn't work? It seems like it should work perfectly fine... Hopefully understanding whats wrong will bring me a step closer to figuring this out
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将其添加到
String[]
,然后再添加到Vector
:如果您需要不同的日期格式,请查看
SimpleDateFormat
。与具体问题无关,也许您正在处理十年前的应用程序,但您是否意识到这段代码充满了过时的 API 和不良实践?
Just add it to
String[]
before adding toVector<String[]>
:If you need a different date format, look at
SimpleDateFormat
.Unrelated to the concrete problem, perhaps you're dealing with a decade old application, but do you realize that this code is full of obsolete APIs and poor practices?