0

I am trying to implement a Servlet with JDBC.
My idea is that when user log in using the login form, he will be able to see the list off available databases in the MySQL. So I'm generating that dynamically on Servlet. That databases are displayed to the user using dropdown list.
Now when user clicks on any of the available databases I am using JavaScript to check which database is selected by the user. Now I want to retrieve that value into my servlet from JavaScript. Based on the selected database i want to display the list of available tables in the database to the user. Is this possible? If not then is there any other way to do so??

Here is the sample code that generates drop down menu dynamically.

                ResultSet r = dbmetadata.getCatalogs();
                ResultSetMetaData metadata = r.getMetaData();
                int colCount = metadata.getColumnCount();
                int i;

                out.println("<select id='db' name='db'onChange= 'myFunction();'>");
                out.println("<option value = -1>--Select Database--</option>");
                int value = 1;
                while(r.next())
                {   
                    for(i=1;i<=colCount;i++)
                    {   
                        out.println("<option value = " + value + ">" +  r.getString(i)  + "</option>");
                    }
                    k++;
                }
                out.println("</select>");

JavaScript code looks something like this:

function myFunction()
{
var select = document.getElementById("db");
var dbSelected = select.options[select.selectedIndex].text;
var x = document.getElementById("dbs");
x.innerHTML = "You have selected " + dbSelected;
};

Thank You :)

Harsh Jani
  • 13
  • 3

1 Answers1

1

You can send a post request to servlet by

  // Send the data using post
    var posting = $.post("/GetAllTables",{dbname:"myydb"});

    // When the POST request is done..
   // data: The output printed in servlet
     posting.done(function(data) {

            // Put the results in a div
            $("#view").append(data+"<br/>");

           //you can also take data string and split it with ,(comma) and append this tablenames to a dropdown etc.

     });

in servlet

String dbName=request,getParameter("dbName");

//generate all table name in string like

String tables="table1,table2,table3"

// Get PrintWriter obj using getWriter() in HttpServletResponse
PrintWriter pw=res.getWriter();

// Print, that's it!!
pw.println(tables);

You can also use ajax in place of post request and send also can send table name in json

Also check Using Post,Using Ajax

Community
  • 1
  • 1
singhakash
  • 7,891
  • 6
  • 31
  • 65
  • Sending post request will refresh the page. Am i right?? Isn't is possible directly to retrieve value from the JavaScript file?? or Is there any way to retrieve parameter from the current html servlet file?? – Harsh Jani Feb 09 '15 at 18:10
  • It will not refresh the page.You can also directly access db but thats a bad practice i will not suggest that but if you want to do it check http://stackoverflow.com/a/857688/3841803 – singhakash Feb 09 '15 at 18:16
  • So it is not secure and not recommended. How do i carry out my task?? I don't see any other way to do this. Can you provide me a link to fiddle for a simple example using the above method you told. If you don't mind!! – Harsh Jani Feb 09 '15 at 18:20
  • Just try the post or ajax methods above given in myanswer.Fiddle do not provide a server side code checking so I cant give you a fiddle.You should try then if you have any problem then create a new question.Someone will help you and if you stuck again then leave a comment here of your question I will try to help:) – singhakash Feb 09 '15 at 18:28
  • Thank you for the help. I am trying the jQuery method that you told. Sorry if i asked some silly questions. – Harsh Jani Feb 09 '15 at 18:31
  • Thank You...@singhakash...I got it working through AJAX :) Accepted Your Answer :) – Harsh Jani Feb 10 '15 at 19:49