从JSP页面获取Array值到外部JavaScript页面

时间:2014-11-12 09:58:42

标签: javascript

我想以树形结构在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);
}

1 个答案:

答案 0 :(得分:1)

您应该能够从请求查询字符串parmaters中检索 dbname 。 这可以使用:

完成
request.getParameter("your-param");

在您的情况下,它应该是 dbname 参数,您在JS文件中的URL中注入:

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>