admin管理员组

文章数量:1415698

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>

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>
Share Improve this question asked Oct 8, 2014 at 4:29 anishanish 7,44814 gold badges84 silver badges153 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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.

本文标签: javascriptHow to access java servlet session attribute in angular js applicationStack Overflow