在特定时间间隔之后提交jsp页面

时间:2013-11-27 16:11:14

标签: java javascript jsp servlets

我使用了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>

2 个答案:

答案 0 :(得分:1)

错误在于where子句应该是这样的: -

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

此外,在将代码传递给jsp

之前,应该在servlet中执行此代码

答案 1 :(得分:0)

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

您只提供一次价值。你的意思是它有价值吗

 int t=120000;

而不是数据库中的内容?如果是这样,你确定没有错误被抛出?

顺便说一下,这不是编写Web应用程序的最佳方式 - 所有这些都在jsp中 - 尽管它有效,但最好是为数据库等制作servlet和POJO / helper .java文件。确保你的tomcat / app服务器是每次重新启动时都会清除临时文件夹 - 以确保它采用最新的jsp。

在jsp中可以有一个类似'Version 001'的文本,并手动增加,以确保正确的代码版本正确运行。


如果您没有记录器,请使用记录器或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注入攻击。看看owasp https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet