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

从JSP页面到外部JavaScript页面获取数组值,javascript,Javascript,我想以树状结构显示MySQL中的数据库 我有一个JSP页面来获取数据库名称,还有一个JavaScript页面以树格式显示它 由于我是web应用程序开发的新手,我不知道如何在不使用jQuery的情况下将数据库名称从JSP页面获取到JavaScript页面。我需要为此使用jQuery吗 demoframeset.html <!---------------------------------------------------------------> <!-- Copyright

我想以树状结构显示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>


您应该能够从请求查询字符串parmaters中检索dbname。 这可以通过以下方式实现:

request.getParameter(“您的参数”);
在您的情况下,它应该是dbname参数,您正在JS文件的URL中注入该参数:

试试看{
String dbName=request.getParameter(“dbName”);//检索dbName参数
字符串responseText=“”;
字符串文本=”;
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
Connection conn=DriverManager.getConnection(“jdbc:mysql://localhost/“+dbName/*在架构URL*/,“root”,“”)中使用dbName;
DatabaseMetaData dbmd=conn.getMetaData();
ResultSet ctlgs=dbmd.getCatalogs();
while(ctlgs.next()){
text+=ctlgs.getString(1)+“,”;
}
}捕获(例外e){
out.println(e);
}
编辑: 否则,您可以简单地在JSP中显示数据库名称,而无需任何外部请求调用,因为您已经将数据库名称填充到一个由逗号分隔的
字符串中,所以您可以使用
标记从
字符串
中迭代拆分的数组文本



  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
  }
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);
}