Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am learning Angular.js. I tried to search many existing questions but could not find one matching question or answer.

I am sending a GET request to a java servlet and expecting an object from it. Here is the angular $http GET request

        $http({
            method:'GET',
            url : '/JSONWebApp/json/?fqn=' + $scope.fqn
        }).
            success(function(data, status, header, config){
                console.log('Success');
                console.log("data - "+data);
                console.log("status - " + status);
                console.log("header - " + header);
                console.log("config - " + config);
            }).
            error(function(data, status, header, config){
                console.log('Error');
            });

On java servlet, here is what I write in doGet method

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       System.out.println("Incoming Request URI : " + request.getRequestURI());

   **User user = new User();
         request.setAttribute("user", user);
   RequestDispatcher view = request.getRequestDispatcher("/html/index.html");
   view.forward(request, response);**
}

When the response is returned from this servlet, it goes to success of $http service in angular side. From where I will get the user object which I sent from server.

When I add the console.log to print the data, it prints the /html/index.html contents which is where I am forwarding.

Based on my research, my angular code is correct but I am not setting things correctly in java servlet.(Does this help - How to access json return values in angular.js action success callback)

Thanks in advance.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You need to serialize your User object into a JSON string and put that in the response body. A library like Jackson can help but before you add that in and confusing yourself more just serialize it into a JSON string yourself. Don't forget to set the Content-Type header.

share|improve this answer
    
JSON is fine but how do we write it to request body in servlet? –  RajaM Feb 26 at 17:23
    
I have not worked with the servlet API directly for a long time but it will be something similar to response.getWriter().print("{\"name\": \"RajaM\"}"). Also, do you need to use Servlets? If it's not a business requirement there are easier ways to create a JSON API. –  Loc Nguyen Feb 26 at 20:30
    
That's actually correct. I was able to send JSON to angular. Just one correction: In addition to writing json to response.writer we do not have to dispatch to any of the view otherwise will will again be sending the html page as data. We just have to flush out the writer after setting the json to response. Thanks Loc. –  RajaM Feb 27 at 7:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.