I am a beginner to java ee and angular JS, I found a code from a website (http://www.simplecodestuffs.com/angularjs-interacting-with-java-servlet-using-json/) I am trying to implement it in eclipse. but when i click the button "fetch data from server" the output doesnot appear as expected
Output:
First Name: {{person.firstName}}
Last Name: {{person.lastName}}
here is my JSP :
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>AJAX with Servlets using AngularJS</title>
<script type="text/javascript" src="jas/angular.min.js"> </script>
<script>
var app = angular.module('myApp', []);
function MyController($scope, $http) {
$scope.getDataFromServer = function() {
$http({
method:'GET',
url:'http://localhost:8080/Angular/AngularJsServlet'
}).success(function(data, status, headers, config) {
$scope.person = data;
}).error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
};
};
</script>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="MyController">
<button ng-click="getDataFromServer()"> Fetch data from server </button>
<p>First Name: {{person.firstName}}</p>
<p>Last Name: {{person.lastName}}</p>
</div>
</div>
</body>
</html>
here is my servlet:
@WebServlet("/AngularJsServlet")
public class AngularJsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AngularJsServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PersonData personData = new PersonData();
personData.setFirstName("Mohaideen");
personData.setLastName("Jamil");
String json = new Gson().toJson(personData);
response.setContentType("application/json");
response.getWriter().write(json);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);
}
}