3

I'm trying to display data in the select tag, where the data are coming form the database. I'm using servlet to fetch data from database. but I'm not getting how to call the servlet from angularjs and display that data in the select tag.

I'm here sharing my code please help me out.

servlet1.java code:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    PrintWriter out = response.getWriter();
    Connection con = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");

        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/apm", "root", "root12345");
        Statement st = con.createStatement();
        ResultSet rs;

        rs = st.executeQuery("select machine_name from machines");

        int i = 1;
        String res = "[",separator = "";

        while (rs.next()) {

                res += separator + rs.getString(1) ;
                separator = ",";
            }

         res += "]" ;
         out.println(res);
    }}

servlet2.java code:

try{
     Class.forName("com.mysql.jdbc.Driver");
     con = DriverManager.getConnection("jdbc:mysql://localhost:3306/apm",   "root", "root12345");
        Statement st = con.createStatement();
        ResultSet rs;
        String start = request.getParameter("start");
        String index = request.getParameter("index"); 
        String tablename = request.getParameter("tablename");
        if (start == null) {
            start = "0";
        }
        if (index == null) {
            index = "1";
        }

        if (tablename == null) {
            tablename = "data";
        }

        String column = "";
        boolean first = true;

        rs = st.executeQuery("show columns from" + tablename );

        int i = 1;
        String res = "[",separator = "";
        while (rs.next()) {

            res += separator + rs.getString(1) ;
            separator = ",";
        }
        res += "]" ;
     out.println(res);
      }

angular js code:

myApp.controller('MyCtrl', ['$scope', function ($scope) {
        $scope.blocs = [];
        $scope.fields = [];
        $scope.addNewField = function (index) {
            alert(index);
            $scope.blocs[index].fields.push({
                sensor : "",
                sensor1 : "",
                sensor2 : ""
            });
        };
        $scope.addBloc = function () {
            $scope.blocs.push({
                fields : []
            });
        };
    }
]);

jsp code:

<div ng-controller="MyCtrl">
    <button ng-click="addBloc()">
    Add Machines
    </button>
    <div ng-repeat="bloc in blocs">
        <div>
            <select>
                <option>---select machine--</option>
            </select>
            <button ng-click="addNewField($index)">
                Add sensors
            </button>
        </div>
        <div ng-repeat="field in bloc.fields">
            <select ng-model="field.sensor">
                <option value="sensor1">sensor1</option>
                <option value="sensor2">sensor2</option>
            </select>
            <input type="text" ng-model="field.sensor1">
            <input type="text" ng-model="field.sensor2"> {{field.sensor}}   {{field.sensor1}} {{field.sensor2}}
        </div>
    </div>
</div>
  1. in the 1st select tag I need to get data from 1st servlet code (i.e servlet1.java).

  2. in the 2nd select tag I need to get data from the 2nd servlet code (i.e servlet2.java).

Please any1 help me out ... :(

1 Answer 1

2

You can use $http service, for example:

var servlet_url = "...";
$http.get(servlet_url)
.then(function(response) {
    $scope.myList = response.data;
    $scope.data = { selected: $scope.myList[0] };
});

and then bind the <select> to data retrieved from servlet using ngOptions directive:

<select ng-model='data.selected' ng-options='item in myList'></select>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.