Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

java - AngularJS http POST to Servlet

I went through a lot of StackOverflow answers and googled a lot but still could not find why my post request is not working.

This is my jsp:

<div class="container">
      <form class="form-signin" ng-controller="MyController">
        <h2 class="form-signin-heading">Please sign in</h2>
        <label for="username" class="sr-only">Username</label>
        <input type="text" id="username"  ng-model="user.name" class="form-control" placeholder="Username" required autofocus>
        <label for="password" class="sr-only">Password</label>
        <input type="password" ng-model="user.password" id="password" class="form-control" placeholder="Password" required>
        <button class="btn btn-lg btn-primary btn-block" ng-click="login()" type="submit">Sign in</button>
      </form>
    </div> 

This is my controller:

app.controller('MyController', function($scope, $http) {

    $scope.login = function() {
        console.log($scope.user);
        $http({
            method : 'POST',
            url : 'login',
            data : $scope.user,
            headers: {
                'Content-Type': 'application/json'
            }
        }).success(function(data) {
            console.log(data);
        }).error(function(data) {
            console.log(data);
        });
        console.log("POST done");
    };
});

And my servlet:

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

        System.out.println("inside do POST");
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonObject obj = (JsonObject) parser
                .parse(request.getParameter("data"));
        Iterator it = (Iterator) obj.entrySet();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        System.out.println("over");
    }

I keep getting this Null pointer exception

java.lang.NullPointerException
    at java.io.StringReader.<init>(StringReader.java:50)
    at com.google.gson.JsonParser.parse(JsonParser.java:45)
    at com.zookeeperUI.controller.Login.doPost(Login.java:40)

Please tell me what am I doing wrong here .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your request does not contain a URL parameter named "data", therefore request.getParameter("data") returns null and you get the NullPointerException.

You try to send a Javascript object via URL parameters which does not go well with non-shallow objects.

I would recommend to send the data as request payload:

JsonObject obj = (JsonObject) parser.parse(request.getReader());

On the client you need to make sure that your data is sent as proper JSON:

$http({
        method : 'POST',
        url : 'login',
        contentType: 'application/json',
        data : JSON.stringify($scope.user),
    })...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...