使用jsp将值传递给settimeout方法

时间:2013-11-27 07:55:13

标签: java jsp servlets jdbc settimeout

我是jsp和servlets的新手并且正在进行在线考试项目。我希望在特定时间间隔(从数据库中获取)之后提交考试(jsp页面)。我无法从数据库中获取时间,而settimeout方法只获取时间变量的初始值。请参阅下面的代码。 谢谢。

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<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+"");  
while(r1.next())
       {
      time=r1.getString("duration");
t=Integer.parseInt(time)*60000;         
  }
      r1.close();
  }
        catch(Exception e1)
        {
        response.setContentType("text/html");
        out.println(e1.toString());
        }
 %>
    <body onload="setTimeout('submitForm()',<%=t%>)">
         <%

        response.setHeader("Cache-Control","no-cache");
  response.setHeader("Cache-Control","no-store");
  response.setHeader("Pragma","no-cache");
  response.setDateHeader ("Expires", 0);

  if(session.getAttribute("no")==null)
      response.sendRedirect("index.jsp");
if((Boolean)session.getAttribute("valid")==false)
      response.sendRedirect("shome.jsp");
%>

  <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"> &copy SAUMYARAJ ZALA</div>
    </body>
</html>  

2 个答案:

答案 0 :(得分:1)

我认为你应该检查你的where子句: -

r1=s1.executeQuery("select duration from tests where testname='"+ts+"'");

答案 1 :(得分:0)

尝试更改此内容..

<body onload="setTimeout('submitForm()',<%=t%>)">

out.println("<body onload='setTimeout('submitForm()',"+t+")'>);

并确保获得t变量的值。