【问题标题】:Passing a value to a settimeout method using jsp使用jsp将值传递给settimeout方法
【发布时间】:2013-12-12 18:12:51
【问题描述】:

我是 jsp 和 servlet 以及制作在线考试项目的新手。我希望在特定时间间隔(从数据库中获取)之后提交考试(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>  

【问题讨论】:

  • Sys.out 这个看看:time=r1.getString("duration");
  • 在哪里查看结果?
  • 控制台。我的意思是 System.out.println(time);
  • 输出中没有显示任何内容。我也试过日志(时间)
  • 所以现在没有进入里面写System.out.println("select duration from tests where testname="+ts+"")所以查询将在控制台中打印。然后只需在 sql 浏览器查询中执行此查询,看看它给出的结果。

标签: java jsp servlets jdbc settimeout


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    尝试改变这个..

    &lt;body onload="setTimeout('submitForm()',&lt;%=t%&gt;)"&gt;

    out.println("&lt;body onload='setTimeout('submitForm()',"+t+")'&gt;);

    并确保您获得 t 变量的值。

    【讨论】:

    • 我认为没有任何区别。 setTimeout 方法成功获取了我的初始化值。
    猜你喜欢
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多