-1

I have a strange situation as follows:

  1. The Web-Client application I'm working is based on JQuery and AngularJS,

  2. I have deployed a TOMCAT server (8.5) and a servlet (JAVA-based developed on Eclipse).

The code sending the requests from follows:

var MyApp = angular.module('MyApp'); 
MyApp.factory('DB_Services', ['$http' , '$q' , function($http , $q) {
    var l_Result ;
    var DB_Services = function(p_URL_Root , p_Query) {
        var l_deferred = $q.defer();
        var l_params   = JSON.stringify(p_Query) ;
        var l_url      = "http://localhost:8080/MEKUWQDispatcher/DispatcherClass";
        var l_params   = 'request=' + encodeURIComponent(JSON.stringify(p_Query)) ;        
        var req = { url    : l_url, 
                    method :"GET", 
                    timeout:10000 , 
                    headers: { 
                        'Content-Type': 'application/json ; charset=UTF-8'
                    }, 
                    data:l_params
                  } ;
        $http(req ).
                    success(function(data, status, headers, config) {
                         l_deferred.resolve(data);
                    }).
                    error(function(data, status, headers, config) {
                         l_deferred.resolve(status);
                    });
                    return l_deferred.promise;
        return l_deferred.promise;
    } ;

    return DB_Services;

}]);

Now, the Servlet includes both GET and POST methods. The GET method works in two ways: If the request contains data, it uses the received data to invoke a DB stored procedure. If no data in the request, it invokes a hardcoded select statement (this second behavior is the simple way I found to verify proper connection to the DB, and it would never be activated in real life; moreover, I may eventually delete that part from the servlet's code).

When using a browser (IE, CHROME, all the same result) and enter the address:

http://localhost:8080/MEKUWQDispatcher/DispatcherClass

I get the result of the hardcoded select statement as expected. If, on the other hand, I enter some data to the request, something like:

http://localhost:8080/MEKUWQDispatcher/DispatcherClass?data="request={blabla}"

I get error 400 (bad request).

Now, switching to the javascript, no patter what I do (with or without data) I always get the result of the hardcoded select statement.

I also added the declaration of the servlet within the web.xml file as follows:

<servlet>
    <servlet-name>MEKUWQDispatcher</servlet-name>
    <servlet-class>MEKUWQDispatcher.DispatcherClass</servlet-class>
</servlet>


<servlet-mapping>
    <servlet-name>MEKUWQDispatcher</servlet-name>
    <url-pattern>/MEKUWQDispatcher</url-pattern>
</servlet-mapping>
halfer
  • 19,824
  • 17
  • 99
  • 186
FDavidov
  • 3,505
  • 6
  • 23
  • 59
  • The double quote and curly brace characters are not valid in a URL. See [this answer](http://stackoverflow.com/questions/7109143/what-characters-are-valid-in-a-url) for more details. It looks like you're doing the correct thing in the Javascript - encodeURIComponent. On the Java side you'll have to decode that too. – stdunbar Mar 15 '17 at 15:09

1 Answers1

-2

Since the original post I made several changes to the service code, being the following a copy of the new code that works like a charm (note also that I switched from GET to POST method):

var MyApp = angular.module('MyApp'); 
MyApp.factory('DB_Services', ['$http' , '$q' , function($http , $q) {
    var DB_Services = function(p_URL_Root , p_Query) {
    var l_deferred = $q.defer();
    var l_params   = JSON.stringify(p_Query) ;
    var l_url      = "http://localhost:8080/MEKUWQDispatcher/DispatcherClass";
    var req = { url    : l_url, 
                method :"POST", 
                timeout:600000 , 
                headers: { 
                    'Content-Type': 'application/json ; charset=UTF-8'
                }, 
               params:{request:p_Query}
              } ;
    $http(req ).
                success(function(data, status, headers, config) {
                     l_deferred.resolve({Server_Response:data , Server_Status: status});
                }).
                error(function(data, status, headers, config) {
                     l_deferred.resolve(status);
                });
                return l_deferred.promise;
    return l_deferred.promise;
} ;

    return DB_Services;

}]);
FDavidov
  • 3,505
  • 6
  • 23
  • 59