ajax调用后刷新整个页面

时间:2014-11-20 14:47:53

标签: javascript ajax jsp

我想在没有jquery的情况下执行ajax调用后重新加载同一页面。这可能吗?在我的代码中,我将删除表中的数据库名称。删除dbnames之后,页面不显示dbnames.Is它可能吗?我用window.location =“Databases.jsp”尝试了它但它没有我可以做什么来执行这个任务呢?我也在framset中显示包含数据库名称的表。所以当我查找/删除数据库名称时我也要刷新左侧框架集。(也就是我需要刷新整页)我的代码:

Database.jsp

    <%-- 
    Document   : Databases
    Created on : Nov 10, 2014, 3:08:01 PM
    Author     : user
--%>

<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DatabaseMetaData"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="querywork.DBConnection"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Untitled Document</title>
        <script language="javascript"   type="text/javascript">
  var request;

        function drop()
        {

            var cboxes = document.getElementsByName('ch');
            var len = cboxes.length;
            var dbnam = document.getElementById("hid") .value
            var dbs="";
            var dbname=dbnam.split(",")
            for (var i=0; i<len; i++) {
                if(cboxes[i].checked)
                {
                    dbs=  dbname[i]

                    var url="Delete.jsp?val="+dbs;

                    if(window.XMLHttpRequest){
                        request=new XMLHttpRequest();
                    }
                    else if(window.ActiveXObject){
                        request=new ActiveXObject("Microsoft.XMLHTTP");
                    }

                    try
                    {
                        request.onreadystatechange=getInfo;
                        request.open("GET",url,true);
                        request.send();
                    }
                    catch(e)
                    {
                        alert("Unable to connect to server");
                    }
                }
            }
        }

        function getInfo(){
            if(request.readyState==4){
                var val=request.responseText;
                document.getElementById('amit').innerHTML=val;

            }
        }             

    </script>
</head>


<body>
    <form id="form1" name="form1" method="post" action="">

        <h2>Databases</h2>
        <label>
            <input type="text" name="textfield" value="Database Name" id="dbname" />
        </label>
        <label>
            <input type="submit" name="Submit" value="CREATE" onClick="create(textfield.value)"/>

        </label>
        <p>&nbsp;</p>



        <%
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/", "root", "");

                DatabaseMetaData dbmd = conn.getMetaData();

                ResultSet ctlgs = dbmd.getCatalogs();

        %>


        <table border="1" cellpadding ="2"cellspacing="3">
            <tr>
                <td>

                </td>
                <td>
                    Database Name
                </td>

            </tr>
            <%
                String dbnme = "";
                int i = 0;

                while (ctlgs.next()) {
                    dbnme = dbnme + ctlgs.getString(1) + ",";
                    String name = "ch" + i;
            %>

            <tr>
                <td>
                    <input name="ch" type="checkbox" value="" id="<%=name%>"/>
                </td>
                <td>
                    <%=ctlgs.getString(1)%>
                </td>
            </tr>
            <%
                    i++;
                }%>
        </table>
        <input type="hidden" name="hid" id="hid1" value="<%=i%>"/>
        <input type="hidden" name="hid" id="hid" value="<%=dbnme%>"/>
        <%
            } catch (Exception e) {
                out.println(e);
            }%>
        <font color="blue"><u><label id="lid" onclick="drop()"> Drop</label></u></font>
        <br></br>
        <div id="amit"></div>
    </form>


</body>

Delete.jsp

<%-- 
    Document   : Delete
    Created on : Nov 11, 2014, 9:43:22 PM
    Author     : user
--%>

<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="querywork.DBConnection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    String responseText = "";
    String message = "";
    String dbname = request.getParameter("val");
    boolean flg = false;
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/", "root", "");
    PreparedStatement ps = conn.prepareStatement("DROP DATABASE " + dbname);
    int k = ps.executeUpdate();
    if (k > 0) {
        flg = true;
    } else {
        flg = false;
    }

    if (flg) {
        message = "success";
    } else {
        message = "failed";
    }
    // }
    responseText = "Deleted";
    out.println(responseText);
    //response.sendRedirect("index.html");

%>

<input type="hidden" id="hid" value="<%=message%>"/>
<script>
    var msg=document.getElementById("hid"). value
    alert(msg)    
    window.location='Databases.jsp';

</script>

1 个答案:

答案 0 :(得分:3)

试试这个

location.reload();

window.location.reload()

如果仍然无法运作,那么另一个

window.location.href=window.location.href