anish
anish

Reputation: 7422

How to access java servlet session attribute in angular js application

How to access servlet session attribute in angular js application, in the example the ProductServlet is setting the products in a session attribute, how to acess this variable in the AngularJs application, what is the equivalent of this

public class ProductServlet extends HttpServlet {
.....
...
    List<Products> products = getProducts();// Return the List of the products
    if (session.getAttribute("userName") == null) {
            session.setAttribute("products", products);
        }
......
......
}

My Angular JS controller

 function ProductController($scope, $http)
    {
      $scope.user = {};

      $scope. GetProducts = function() 
      {
        $http({
          method: 'POST',
          url: 'http://localhost:8080/Products',
          headers: {'Content-Type': 'application/json'},
          data:  $scope.user
        }).success(function (data) 
          {
            $scope.status=data;
          });
      };
    }

The HTML

<body>
    <form ng-controller="ProductController" ng-submit="GetProducts()">
    <legend>Get Productsr</legend>


      <button class="btn btn-primary">Get Products</button>
      <br/>
      <label>DISPLAY the List of products</label>
    </form>
  </body>

</html>

Upvotes: 2

Views: 7231

Answers (1)

Thilo
Thilo

Reputation: 262754

You cannot just access server-side data (such as attributes) from Javascript running in the browser.

You'd have to create some web service to expose this data (for example to JSON GET requests).

And if you do that, make sure that no one unauthorized can access this information.

Upvotes: 4

Related Questions