在特定时间间隔后提交jsp页面
Submission of a jsp page after a particular time interval
我使用了setTimeout方法来实现这一点,并传递了一个包含时间的变量,但我的setTimeout方法只获取该变量的初始化值,而不获取从数据库中获取的值。
这是我的代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Givetest</title>
<script type = "text/javascript">
function submitForm() {
document.forms[0].submit();
}
</script>
<script language="JavaScript" src="http://scripts.hashemian.com/js/countdown.js"></script>
</head>
<%
String ts=request.getParameter("testname");
session.setAttribute("tname", ts);
Connection con=null;
Statement s1=null;
Statement s=null;
ResultSet r1=null;
ResultSet r=null;
int t=120000;
String time=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:online_testing");
s=con.createStatement();
s1=con.createStatement();
r=s.executeQuery("select * from "+ts+"");
r1=s1.executeQuery("select duration from tests where testname="+ts+"");
if(r1.next())
{
time=r1.getString("duration");
t=Integer.parseInt(time)*60000;
logger.info(time);
}
else {
logger.info("No row found in db for test " + ts);
System.out.println("No row found in db for test " + ts);
out.println("<br>!! <b>No row found in db </b>for test " + ts + "<br><br><br>");
}
r1.close();
}
catch(Exception e1)
{
response.setContentType("text/html");
out.println(e1.toString());
}
%>
<body onload="setTimeout('submitForm()',<%=t%>)">
<div class="header"></div>
<div class="view" style="color: #050505">
<form action="Givetest" method="post">
<h1 align="center" style="color: #050505"><%=ts%></h1>
<%
int i=1;
while(r.next()){
String a = r.getString("question");
String b = r.getString("option1");
String c = r.getString("option2");
String d = r.getString("option3");
String e = r.getString("option4");
%>
Question <%=i%>:- <label> <%=a%></label><br>
<input type="radio" name="r<%=i%>" value="<%=b%>" checked><label><%=b%></label><br>
<input type="radio" name="r<%=i%>" value="<%=c%>"><label><%=c%></label><br>
<input type="radio" name="r<%=i%>" value="<%=d%>"><label><%=d%></label><br>
<input type="radio" name="r<%=i%>" value="<%=e%>"><label><%=e%></label><br>
<br>
<input type="hidden" name="h" value="<%=ts%>">
<%
i++;
}
r.close();
s.close();
con.close();
%>
<input type="submit" class="button">
</form>
</div>
<div class="copyright" align="center"> © SAUMYARAJ ZALA</div>
</body>
</html>
错误出现在where子句中,该子句应为:-
r1=s1.executeQuery("select duration from tests where testname="+ts+"");
此外,在将此代码传递给jsp 之前,应该在servlet中执行此代码
<body onload="setTimeout('submitForm()',<%=t%>)">
您只给出一次值。你是说它有价值吗
int t=120000;
而不是数据库中的内容?如果是,你确定没有抛出错误吗?
顺便说一句,这不是编写web应用程序的最佳方式-全部使用jsp-尽管它有效,但最好是为数据库等创建servlet和POJOs/helper.java文件。确保每次重新启动时都会清理tomcat/app服务器的临时文件夹,以确保它使用最新的jsp。
在jsp中,可以有一个类似"Version001"的文本,并手动增加它,以便确保运行正确的代码版本。
如果没有记录器,请使用记录器或system.out.println
r1=s1.executeQuery("select duration from tests where testname="+ts+"");
//if should be enough as you will only have 0 or 1 row per test?
if(r1.next())
{
time=r1.getString("duration");
t=Integer.parseInt(time)*60000;
} else{
logger.warn("No row found in db for test " + ts);
//okay for debug
out.println("<br>!! <b>No row found in db </b>for test " + ts + "<br><br><br>");
}
r1.close();
}
catch(Exception e1)
{
response.setContentType("text/html");
out.println("<br><br> <b> ERROR</b>" + e1.toString());
}
sql
testname="+ts+"
非常糟糕。应该使用准备好的语句,或者您正在请求SQL注入攻击。看看owasphttps://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet
相关文章:
- onclick提交按钮JSP
- 在JSP中成功提交表单后清除表单字段
- 在特定时间间隔后提交jsp页面
- 提交 jquery 后删除表单 (JSP&Servlet)
- 页面自动刷新 JSP 时自动提交
- 检索 JSP 脚本变量中的隐藏输入值,而无需提交页面
- 向 JSP 提交 JavaScript 数组
- 从 JSP 中的现有表单提交按钮调用小程序
- 如何在点击/提交表单时调用 JSP 方法
- 我们可以提交两次 jsp 表单吗?
- 在jsp中提交页面后填充弹出窗口
- 使用JSP从外部Javascript文件提交变量
- 单击按钮时JSP表单未提交
- 从jsp页面自动提交html表单
- 在JS中返回false,不停止JSP表单提交
- 即使JavaScript验证返回false, JSP表单仍然提交
- 不能向JSP提交JSON对象
- 为什么javascript自动提交不工作在我的jsp页面
- 在不刷新页面的情况下提交JSP后发出javascript警报
- 提交JSP后页面重新加载延迟