2013-11-27 26 views
1

我是jsp和servlets的新手,正在製作一個Online Exam項目。我想要在特定的時間間隔(從數據庫中提取)後提交考試(jsp頁面)。我無法從數據庫中獲取時間,settimeout方法僅取得時間變量的初始值。請參閱下面的代碼。 Thankyou。使用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> 
+0

Sys.out這一點,並請參閱:時間= r1.getString( 「時間」); –

+0

在哪裏可以看到結果? – sam

+0

控制檯。我的意思是System.out.println(time); –

回答

1

我認爲你應該檢查你的where子句: -

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

嘗試通過改變這個..

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

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

並確保你在t變量的值。

+0

我不認爲有任何區別。 setTimeout方法成功地獲得了我的初始化值。 – sam