Was playing around a bit with AngularJS. Seems like a great way to structure your templates at client vs server-side. After playing around with Jersey and bunch of other Java REST frameworks, I finally decided to go with just a basic Servlet container and have that provide some JSON data over a REST style interface. Just too much badly document weird requirements there in the frameworks, couldn’t really figure out the benefits.. anyway

Getting the data to AngularJS is not hard. Just have the Servlet return the page with the template. Then for dynamically querying data from server in Angular I can make a call such as

    function myController($scope,$http) {
        $http.get("http://localhost:5555/hello").
                success(function(response) {
                    $scope.values = response.my_data;
                }
        );
    }

Using response.variablename I can also put the contents of the returned JSON object up for parsing by other parts of the template, or just use plain “response” for using it as is. But this only covers the simple scenario of having no parameters in the query. What if I need to perform a post with some request parameters?

I tried

    function myController($scope,$http) {
        var data = "msg="+JSON.stringify({count:10});
        $http.post("http://localhost:5555/hello", data).
                success(function(response) {
                    $scope.values = response;
                }
        );
    }

I thought this would allow me to access it in a Servlet using request.getParameter(“msg”). But this just returns null.. Seems to be because the post from AngularJS is not a HTML Form post, so the Servlet container does not parse it as I was expecting. So we need to write our own parser..

  private void showPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // Read from request
    StringBuilder buffer = new StringBuilder();
    BufferedReader reader = req.getReader();
    String line;
    while ((line = reader.readLine()) != null) {
      buffer.append(line);
    }
    String data = buffer.toString();

    log.debug("Latest request:"+data);

    try {
      JSONObject json = new JSONObject(data);
...

And then we have the request data in the “json” variable. And it is better now to forget the “msg=..” part from above attempt, and just send over the plain JSON.

Looking at the REST frameworks, I guess the simplest way would be to just embed the parameters in the request URL such as /hello/my_parameter. Would probably need a ServletFilter to parse all that.. Maybe someday..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s