4 Replies - 966 Views - Last Post: 16 November 2014 - 12:45 AM

#1 Aswani45   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 12-November 14

Return value from jsp page to javascript page

Posted 12 November 2014 - 11:36 PM

I want to display the databases in mysql in a tree structure.

I have a jsp page to get the database name and javascript page to display it in tree format.

As i am new to web application development i dont know how to get the database name from jsp page to javascript page without using jquery.

does i need to use jquery for this purpose?

demoframeset.html

<!--------------------------------------------------------------->
<!-- Copyright (c) 2006 by Conor O'Mahony.                     -->
<!-- For enquiries, please email [email protected]        -->
<!-- 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);
}

can anyone help me?Its very urgent

Thanks in advance... :)

:code:

This post has been edited by Dormilich: 13 November 2014 - 12:50 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Return value from jsp page to javascript page

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2031
  • View blog
  • Posts: 6,203
  • Joined: 15-January 14

Re: Return value from jsp page to javascript page

Posted 13 November 2014 - 08:36 AM

You already have a couple functions (create, statechanged) which will send an ajax request to the JSP page and put the result on the page. It doesn't look like you're calling the create function though. In the statechanged function, you're putting the results on the page but it doesn't look like you're doing anything else there, so it's just going to print the result from the JSP page inside an element with the id "div", assuming you have one on the page. It doesn't look like you have an element with that ID on the page.
Was This Post Helpful? 0
  • +
  • -

#3 Aswani45   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 12-November 14

Re: Return value from jsp page to javascript page

Posted 13 November 2014 - 10:04 PM

Thanks for the reply...You are right.I have forgotten to add the <div> tag.
But on another page i added the <div> tag.but it doesn't show any content from jsp page and i dont know why.can u please tell me the reason?

Databases.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">

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>&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>
   <%
          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>
</html>



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

This post has been edited by laytonsdad: 14 November 2014 - 10:46 AM
Reason for edit:: Please use CODE tags

Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2031
  • View blog
  • Posts: 6,203
  • Joined: 15-January 14

Re: Return value from jsp page to javascript page

Posted 14 November 2014 - 08:14 AM

You need to use code tags when you post code here, it's not easy to read all of that with no formatting.

Check your browser's developer console for error messages from Javascript. You can also use the developer console to look at the ajax request going out and check what the response was.
Was This Post Helpful? 0
  • +
  • -

#5 Aswani45   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 12-November 14

Re: Return value from jsp page to javascript page

Posted 16 November 2014 - 12:45 AM

ok.i have solved my problem..Thanks for your suggestion :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1