使用XMLHTTP刷新问题

时间:2015-01-07 09:45:34

标签: jsp xmlhttprequest

- 我是JSP的新手,我使用XMLHTTP从mySQL数据库中检索数据,但是在更新数据之后,它只显示以前的数据而不是更新的数据,请帮我解决问题。我有一个单选按钮,

<td class="inputs_new"> <div id = "UT2">

                        <%
                        for (int i = 0; i < thinClientName.size(); i++) {

                        %>
                        <input type = "radio"  class = "my-button2"  onclick="get()" id = "thinClnt_rad" name ="thinClnt_rad" value = "<%=thinClientName.get(i)%>" >
                        <% 
                        out.println(thinClientName.get(i));
                        }
                        %>



    </div> 

     //Get function

      function get()
        {

        var save_thin = "";
    for (var i = 0; i < document.getElementsByName('thinClnt_rad').length; i++)
       {
            if (document.getElementsByName('thinClnt_rad')[i].checked)
            {
                 save_thin =  document.getElementsByName('thinClnt_rad')[i].value;
            }
        }   

            var xmlhttp=  GetXmlHttpObject1();
            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {

                var respTest = xmlhttp.responseText;
                var res = respTest.split(";");

                document.getElementById("thinClientName").value=res[0];
                document.getElementById("Model").value=res[1];
                document.getElementById("SrNo").value=res[2];
                document.getElementById("ProductID").value=res[3];
                document.getElementById("sprNo").value=res[4];
                document.getElementById("MacAdd").value=res[5];
                document.getElementById("IPAdd").value=res[6];
                document.getElementById("Script").value=res[7];
             document.getElementById("VedName").value =res[10] ;
                document.getElementById("playtime").value = res[11] ;

                 var sid_2=res[8];

    var options= document.getElementById('lstLocation').options;

    for (var i= 0; i <options.length ; i++)
         {
             if (options[i].text==sid_2) {

                document.getElementById("lstLocation").selectedIndex = i; 
            }
        }


      var sid_21=res[9];

    var options= document.getElementById('state').options;

    for (var i= 0; i <options.length ; i++)
         {
             if (options[i].text==sid_21) {

                document.getElementById("state").selectedIndex = i; 
            }
        } 


            }

              }


            xmlhttp.open("GET","GetData.jsp?q="+save_thin,true);
            xmlhttp.send(null);

        }
    function GetXmlHttpObject1()
    {
        if (window.XMLHttpRequest)
        {
           return new XMLHttpRequest();
        }
        if (window.ActiveXObject)
        {
          return new ActiveXObject("Microsoft.XMLHTTP");
        }
     return null;
    }

   // and getData.jsp
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
        <%@ page import = "java.io.*,java.sql.*" %>

        <%@page import= "jdbc.DBConnection" %>


    <%


            String str=request.getParameter("q");


            try
            {
                DBConnection con_obj    = new DBConnection();
                Connection con = con_obj.getConnection();
                Statement stmtThinInfo = null;
                Statement stmt_play = null;
                ResultSet rsThinInfo = null;
                ResultSet rs_play = null;
                String q =request.getParameter("q");
                stmtThinInfo = con.createStatement();

            rsThinInfo = stmtThinInfo.executeQuery("select * from thinclient_tbl where name = '"+q+"'"); 
            while (rsThinInfo.next())
            {


                stmt_play = con.createStatement();
                rs_play = stmt_play
                        .executeQuery("Select * from thin_videotable where Thin_Client = '"
                        + q + "'");
                String outPut;
                if (rs_play.next()) {

                 outPut =  rsThinInfo.getString("name") + ";" + rsThinInfo.getString("modell") + ";" +  rsThinInfo.getString("serialnumber") +  ";" +  rsThinInfo.getString("product_id") +  ";" +  rsThinInfo.getString("spare_nr")+  ";" +  rsThinInfo.getString("MAC-Adresse") +  ";" +  rsThinInfo.getString("IP-Adresse") +  ";" +  rsThinInfo.getString("script") + ";" +  rsThinInfo.getString("location")+ ";"  +  rsThinInfo.getString("state") +  ";" +  rs_play.getString("VideoName") + ";" +  rs_play.getString("Video_time");
                response.getWriter().write(outPut);
    System.out.println(outPut);
                }
                else
                {
                    outPut =  rsThinInfo.getString("name") + ";" + rsThinInfo.getString("modell") + ";" +  rsThinInfo.getString("serialnumber") +  ";" +  rsThinInfo.getString("product_id") +  ";" +  rsThinInfo.getString("spare_nr")+  ";" +  rsThinInfo.getString("MAC-Adresse") +  ";" +  rsThinInfo.getString("IP-Adresse") +  ";" +  rsThinInfo.getString("script") + ";" +  rsThinInfo.getString("location")+ ";"  +  rsThinInfo.getString("state") +  ";" ;
                    response.getWriter().write(outPut);
                    System.out.println(outPut);
                }

            }
            }
            catch(Exception e)
            {

                e.printStackTrace();
            }



    %>

如果您有任何建议,请检查我的代码并告诉我。

1 个答案:

答案 0 :(得分:0)

你在尝试使用IE吗? 因为IE以侵略性缓存而闻名,并且将在相同的AJAX查询中返回相同的结果,直到刷新整个页面。

您可以在查询字符串中包含随机标识符。 试试这个:

xmlhttp.open("GET","GetData.jsp?q="+save_thin+"&rand="+Math.random(),true);