1

Yesterday I tried using Tomcat and Servlets for the first time (I come from IIS/C#/MVC).

I'm also using AngularJS and Guice.

I've made a Servlet that has a single method:

@Singleton
@SuppressWarnings("serial")
public class CommandServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println(req.getParameterMap());
    }

}

I've made a service in Angular that looks like the following:

app.factory('console', [ '$http', function($http) {
    var console = {};

    console.execute = function(gameId, command) {
        $http.post("Command/", {
            gameId : gameId,
            command : command
        }).success(function(data, status, headers, config) {

        }).error(function(data, status, headers, config) {

        });
    };

    return console;
} ]);

In my controller I inject the service and expose it to the view via an "execute" function on the scope:

app.controller('PlayController', function($scope, console) {
    $scope.consoleIn = "";

    $scope.gameId = 1;

    $scope.execute = function(command) {
        $scope.consoleOut = console.execute($scope.gameId, command);
    };
});

And in my view I have a button which calls the function and passes in text from an input element:

<input ng-model="consoleIn" type="text">
<button class="btn" type="button" ng-click="execute(consoleIn)">Go!</button>

For some reason the console on my Tomcat server is printing an empty Map ({}) without the parameters being passed with the POST request. When I look at the network tab in Chrome's Console thingy and see that the parameters are being sent ({"gameId":1,"command":"a"}), so my question is 1) Am I doing the right thing to get the values out of the POST request (getParameterMap() ?) and 2) if I am doing the right thing, what am I doing wrong so that the request my browser makes isn't getting to my servlet properly?

EDIT:

I ended up using Jersey as my container (I think it's called) instead of Java's default Servlets. I did this after seeing Jersey popping up with Guice often on Google, thinking there would be sufficient documentation to get the two of them working together. There are several examples and I sort of drew on several, especially this one.

The snag I ran into getting it to work was this, but now everything's good to go.

Overall I'd say that if you like Guice for your DI and want to make a Java website, Jersey is good. It appears to be for a different more specialized functionality of RESTful services than regular servlets - but that's what I needed, anyway. From my un-scientific Googling observations besides Tomcat there's Grizzly and Jetty that are popular as well - you may want to look into them if you're having trouble with Tomcat.

I hope this edit saves someone the hours I spent yesterday and today getting it to work.

Community
  • 1
  • 1
Jesus is Lord
  • 14,971
  • 11
  • 66
  • 97

1 Answers1

0

What does it actually print? Some of the Tomcat Map implementations don't print their contents in their toString() method. Try another way of seeing what's in it. You'll find its all there.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks for replying. Debugging it with a breakpoint at the line showed the same thing (empty map). I'm actually looking into Jersey and resources right now instead of the Servlets (since I'm making a client-side application that just needs to pass and receive JSON). But the server literally just printed "`{}`" (without the quotes), to answer your question. – Jesus is Lord Jan 31 '13 at 00:06
  • @WordsLikeJared Well your JQuery code looks OK to me, much the same as mine which works. Have you tried iterating over the map? Calling req.getParameter() for 'command' and 'gameId'? – user207421 Jan 31 '13 at 00:45
  • @EJP He is using angular.js not jquery.js and he is passing JSON to Tomcat. It should be form-urlencoded. – jdb Jan 31 '13 at 09:06