gpt4 book ai didi

javascript - 从JSP页面获取数组值到外部JavaScript页面

转载 作者:行者123 更新时间:2023-12-03 11:26:50 25 4
gpt4 key购买 nike

我想以树形结构显示MySQL中的数据库。

我有一个 JSP 页面来获取数据库名称和 JavaScript 页面以树形格式显示它。

由于我是 Web 应用程序开发新手,我不知道如何在不使用 jQuery 的情况下从 JSP 页面到 JavaScript 页面获取数据库名称。我需要使用 jQuery 来实现此目的吗?

demoframeset.html

<!--------------------------------------------------------------->
<!-- Copyright (c) 2006 by Conor O'Mahony. -->
<!-- For enquiries, please email GubuSoft@GubuSoft.com. -->
<!-- Please keep all copyright notices below. -->
<!-- Original author of TreeView script is Marcelino Martins. -->
<!--------------------------------------------------------------->
<!-- This document includes the TreeView script. The TreeView -->
<!-- script can be found at http://www.TreeView.net. The -->
<!-- script is Copyright (c) 2006 by Conor O'Mahony. -->
<!--------------------------------------------------------------->
<!-- Instructions: -->
<!-- - Through the <STYLE> tag you can change the colors and -->
<!-- types of fonts to the particular needs of your site. -->
<!-- - A predefined block with black background has been -->
<!-- made for stylish people :-) -->
<!--------------------------------------------------------------->

<HEAD>

<!-- This is the <STYLE> block for the default styles. If -->
<!-- you want the black background, remove this <STYLE> -->
<!-- block. -->
<STYLE>
BODY {
background-color: white;}
TD {
font-size: 10pt;
font-family: verdana,helvetica;
text-decoration: none;
white-space:nowrap;}
A {
text-decoration: none;
color: black;}
.specialClass {
font-family:garamond;
font-size:12pt;
color:green;
font-weight:bold;
text-decoration:underline}
</STYLE>

<!-- If you want the black background, replace the contents -->
<!-- of the <STYLE> tag above with the following...
BODY {
background-color: black;}
TD {
font-size: 10pt;
font-family: verdana,helvetica;
text-decoration: none;
white-space:nowrap;}
A {
text-decoration: none;
color: white;}
<!-- This is the end of the <STYLE> contents. -->

<!-- Code for browser detection. DO NOT REMOVE. -->
<SCRIPT src="ua.js"></SCRIPT>

<!-- Infrastructure code for the TreeView. DO NOT REMOVE. -->
<SCRIPT src="ftiens4.js"></SCRIPT>

<!-- Scripts that define the tree. DO NOT REMOVE. -->
<SCRIPT src="demoFramesetNodes.js"></SCRIPT>

</HEAD>

<BODY topmargin="16" marginheight="16">

<!------------------------------------------------------------->
<!-- IMPORTANT NOTICE: -->
<!-- Removing the following link will prevent this script -->
<!-- from working. Unless you purchase the registered -->
<!-- version of TreeView, you must include this link. -->
<!-- If you make any unauthorized changes to the following -->
<!-- code, you will violate the user agreement. If you want -->
<!-- to remove the link, see the online FAQ for instructions -->
<!-- on how to obtain a version without the link. -->
<!------------------------------------------------------------->
<DIV style="position:absolute; top:0; left:0;"><TABLE border=0><TR><TD><FONT size=-2><A style="font-size:7pt;text-decoration:none;color:silver" href="http://www.treemenu.net/" target=_blank>Javascript Tree Menu</A></FONT></TD></TR></TABLE></DIV>

<!-- Build the browser's objects and display default view -->
<!-- of the tree. -->
<SCRIPT>initializeDocument()</SCRIPT>
<NOSCRIPT>
A tree for site navigation will open here if you enable JavaScript in your browser.
</NOSCRIPT>

</BODY>

demoFrameSetNodes.js

  USETEXTLINKS = 1
STARTALLOPEN = 0
ICONPATH = ''
foldersTree = gFld("<i>Databases</i>", "demoFramesetRightFrame.html")
foldersTree.treeID = "Frameset"
aux11 = insFld(foldersTree, gFld("New", "Databases.jsp"))

var xmlHttp
function create()
{
xmlHttp=CreateXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}

var url="new1.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("div").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
}

new1.jsp

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

DatabaseMetaData dbmd = conn.getMetaData();

ResultSet ctlgs = dbmd.getCatalogs();
while (ctlgs.next()) {
text += ctlgs.getString(1) + ",";
}
} catch (Exception e) {
out.println(e);
}

最佳答案

您应该能够从请求查询字符串参数中检索数据库名称。这可以使用以下方法完成:

request.getParameter("your-param");

在您的例子中,它应该是 dbname 参数,您在 JS 文件中的 URL 中注入(inject)该参数:

try {
String dbName = request.getParameter("dbname"); // Retrive the dbname parameter
String responseText = "";
String text = "";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/" + dbName /*Use the dbName in the schema URL*/, "root", "");

DatabaseMetaData dbmd = conn.getMetaData();

ResultSet ctlgs = dbmd.getCatalogs();
while (ctlgs.next()) {
text += ctlgs.getString(1) + ",";
}
} catch (Exception e) {
out.println(e);
}

编辑:

否则,您可以简单地在 JSP 中显示数据库名称,而无需任何外部请求调用,因为您已经将数据库名称填充为 String以逗号分隔,您可以使用 <c:forTokens>标记来迭代您的分割数组 String ; 文字:

<c:forTokens items="${text}" delims="," var="dbname"> 
<c:out value="${dbname}"/><p>
</c:forTokens>

关于javascript - 从JSP页面获取数组值到外部JavaScript页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26884055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com