关于从JavaScript中设置的参数如何传递到Servlet中的问题。
JS中代码如下:<formid="myform"method="post"action="/servlet/WhisperServlet"><inputtype="hid...
JS中代码如下:
<form id="myform" method="post" action="/servlet/WhisperServlet" >
<input type="hidden" name="params1" />
<input type="hidden" name="params2" />
</form>
<script type="text/javascript">
function whispers(){
var wname=prompt("请输入您的名字","");
var wcontent=prompt("请输入您想说的话","");
alert("恭喜您:"+ wname + "\n\n您的悄悄话已发送了。" )
document.forms[0].params1.value = wname;
document.forms[1].params2.value = wcontent;
document.forms[0].submit();
}
</script>
Servlet中代码如下:
String wname = request.getParameter("params1");
String content = request.getParameter("params2");
System.out.println(wname);
System.out.println(content);
String sql = "insert into whisper(wname, content) value(?,?)";
String params[] = {wname, content};
QueryRunner qr = DbHelper.getQuerryRunner();
try {
qr.update(sql, params);
} catch (SQLException e) {
e.printStackTrace();
}
程序能正常运行,我觉得也应该把参数传递到数据库中了的,但不知为什么数据库对于表中属性显示为null? 展开
<form id="myform" method="post" action="/servlet/WhisperServlet" >
<input type="hidden" name="params1" />
<input type="hidden" name="params2" />
</form>
<script type="text/javascript">
function whispers(){
var wname=prompt("请输入您的名字","");
var wcontent=prompt("请输入您想说的话","");
alert("恭喜您:"+ wname + "\n\n您的悄悄话已发送了。" )
document.forms[0].params1.value = wname;
document.forms[1].params2.value = wcontent;
document.forms[0].submit();
}
</script>
Servlet中代码如下:
String wname = request.getParameter("params1");
String content = request.getParameter("params2");
System.out.println(wname);
System.out.println(content);
String sql = "insert into whisper(wname, content) value(?,?)";
String params[] = {wname, content};
QueryRunner qr = DbHelper.getQuerryRunner();
try {
qr.update(sql, params);
} catch (SQLException e) {
e.printStackTrace();
}
程序能正常运行,我觉得也应该把参数传递到数据库中了的,但不知为什么数据库对于表中属性显示为null? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询