如何在 Angular js 应用程序中访问 servlet session 属性,在示例中 ProductServlet 在 session 属性中设置产品,如何在 AngularJs 应用程序中访问此变量,这相当于什么
public class ProductServlet extends HttpServlet {
.....
...
List<Products> products = getProducts();// Return the List of the products
if (session.getAttribute("userName") == null) {
session.setAttribute("products", products);
}
......
......
}
我的 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;
});
};
}
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>
最佳答案
您不能仅通过浏览器中运行的 Javascript 访问服务器端数据(例如属性)。
您必须创建一些 Web 服务来公开此数据(例如 JSON GET 请求)。
如果您这样做,请确保未经授权的任何人都无法访问此信息。
关于java - 如何在 Angular JS 应用程序中访问 java servlet session 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26249230/