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

How to access servlet session attribute in angular js application, in the example the ProductServlet is

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745238426a4618030.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信