在使用AJAX调用向JSP页面发送请求后的代码中,它不会在<div>
标记中显示结果。我的<div>
代码的ID为&#34; div1&#34;。控制权传递给JSP页面。处理完JSP页面后,responseText
中的值不会显示在<div>
标记中。我不明白原因。我特此附上代码......我也可以在&#34; new.jsp&#34;上添加标签。文件?
<%--
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">
try
{
function create(str)
{
var xmlHttp
xmlHttp=CreateXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="new.jsp"
url=url+"?dbname="+str
url=url+"&sid="+Math.random()
// out.print(url)
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send()
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("div1").innerHTML=xmlHttp.responseText
}
}
function CreateXmlHttpObject()
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}
}
catch(err)
{}
</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> </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>
<%
while (ctlgs.next()) {
%>
<tr>
<td>
<input name="ch" type="checkbox" value="" id="" />
</td>
<td>
<%=ctlgs.getString(1)%>
</td>
</tr>
<%}
} catch (Exception e) {
out.println(e);
}%>
</table>
<a href="Delete.jsp">Drop</a>
<br></br>
<div id="div1">ffff</div>
</form>
</body>
new.jsp
<%--
Document : new
Created on : Nov 11, 2014, 12:41:34 PM
Author : user
--%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="querywork.DBConnection"%>
<%@page import="javax.faces.component.behavior.AjaxBehavior"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
try {
String responseText = new String("");
boolean flg = false;
String OptionValue = (String) request.getParameter("dbname");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/", "root", "");
PreparedStatement ps = conn.prepareStatement("create database " + OptionValue);
int k = ps.executeUpdate();
if (k > 0) {
flg = true;
} else {
flg = false;
}
responseText += OptionValue;
} catch (Exception e) {
out.print(e);
}
%>
答案 0 :(得分:1)
这些是我发现的问题:
您正在创建responseText
字符串,但您并未将其写入输出流,因此它不会出现在发送回客户端的响应中。
在try
阻止(new.jsp
)结束后,执行
responseText += OptionValue;
你需要添加
out.print(responseText);
您的按钮属于submit
类型,因此它会提交表单。这意味着在调用AJAX请求后将立即重新加载页面。将return false
添加到onclick
处理程序,如下所示:
<input type="submit" name="Submit" value="CREATE"
onclick="create(textfield.value); return false;"/>
或者将您的按钮从submit
按钮更改为常规按钮:
<input type="button" name="Submit" value="CREATE"
onclick="create(textfield.value)"/>
您需要在var xmlHttp
方法之外声明create
。就像现在一样,您的stateChanged()
方法无法访问您的xmlHttp
变量。