以下代码段将标题提交到数据库。填写文本后,我单击提交,但令我惊讶的是,表格中总是出现空值。我在下面提到了 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>
小服务程序
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 ?