【发布时间】:2012-05-07 22:11:47
【问题描述】:
下面的 sn -p 向数据库提交一个标题。填写文本后,我单击提交,但令我惊讶的是,总是有一个 null 进入表格。我在下面提到了servlet代码和html设计:
<form method="post" action="Handler" enctype="multipart/form-data">
<table>
<tr>
<td> <strong> Leave a caption </strong> </td>
<td> <input type="text" name="caption box" size="40" /></td>
</tr>
<tr colspan="2">
<td> <input type="submit" value="submit caption"/> </td>
</tr>
</table>
</form>
Servlet
String caption = request.getParameter("caption box"); // get the caption from the caption field
HandleCaption hc = new HandleCaption(caption,emailOfTheUser,fileName);
hc.SubmitCaptionToTheDatabase();
类
public class HandleCaption {
private String Caption = null;
private String UserEmail = null;
private String NameOfThe = null;
public HandleCaption(String caption,String email,String filename) {
Caption = caption;
UserEmail = email;
NameOfThe = filename;
}
public void SubmitCaptionToTheDatabase() {
try {
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/DS");
Connection connection = ds.getConnection();
String sqlQuery = "insert into CAPTIONS values ('" + UserEmail + "','" + NameOfThe + "','" + Caption + "')";
PreparedStatement statement = connection.prepareStatement(sqlQuery);
int x = statement.executeUpdate();
}catch(Exception exc) {
exc.printStackTrace();
}
}
}
我尝试打印 servlet 中返回的文本字段的值,甚至打印的 null。为什么文本字段返回 null ?
【问题讨论】:
-
我也无法在该文本字段中的单词之间留出空格!
-
如果
input小部件不使用包含空格的名称会怎样? -
你真的需要 multipart/form-data 吗?
-
@GregKopff 我不明白你
标签: java html database servlets