最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access java servlet session attribute in angular js application - Stack Overflow

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论