Javascript 使用Javascript可以从HTML页面中servlet设置的会话属性中获取值吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6918314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Javascript can you get the value from a session attribute set by servlet in the HTML page
提问by user878195
I have a servlet that forwards to a HTML page, using redirect. Because I am using ajax and php on the html page to do other functions. Can turn into a jsp. Is there a way I can get the name -"poNumber" I get in servlet in the session attributes. I was to get it and display it's value.
我有一个使用重定向转发到 HTML 页面的 servlet。因为我在html页面上使用ajax和php做其他功能。可以转成jsp。有什么方法可以让我在会话属性中的 servlet 中获得名称 -“poNumber”。我要得到它并展示它的价值。
I am new to programming.
我是编程新手。
Can get this working in jsp.
可以让它在jsp中工作。
However need to get this working in a html page. Can I do it with javascript?
但是需要让它在 html 页面中工作。我可以用javascript来做吗?
I have tried:
我试过了:
<script type="text/javascript">
var purchaseOrderNo = session.getAttribrute("pONumb");
document.write("pONumb");
</script> [
This does not output any values on the HTML page.
这不会在 HTML 页面上输出任何值。
Tried:
尝试:
<script type="text/javascript">
var purchaseOrderNo = (String) session.getAttribrute("pONumb");
document.write("pONumb");
</script>
Again get no output on page.
再次在页面上没有输出。
Tried:
尝试:
<script type="text/javascript">
String purchaseOrderNo = (String) session.getAttribrute("pONumb");
document.write("pONumb");
</script>
Again get no output on page?
再次在页面上没有输出?
Can not think of any thing else to try. The servlet that redirects to this HTML page creates and set session attribute pONumb.
想不出还有什么可以尝试的。重定向到此 HTML 页面的 servlet 创建并设置会话属性 pONumb。
回答by Alex Ciminian
No, you can't. JavaScript is executed on the client side (browser), while the session data is stored on the server.
不,你不能。JavaScript 在客户端(浏览器)执行,而会话数据存储在服务器上。
However, you can expose session variables for JavaScript in several ways:
但是,您可以通过多种方式为 JavaScript 公开会话变量:
- a hidden input field storing the variable as its value and reading it through the DOM API
- an HTML5 data attribute which you can read through the DOM
- storing it as a cookie and accessing it through JavaScript
- injecting it directly in the JS code, if you have it inline
- 一个隐藏的输入字段,将变量存储为它的值并通过 DOM API 读取它
- 您可以通过 DOM 读取的 HTML5 数据属性
- 将其存储为 cookie 并通过 JavaScript 访问它
- 直接在 JS 代码中注入它,如果你有内联
In JSP you'd have something like:
在 JSP 中,你会有类似的东西:
<input type="hidden" name="pONumb" value="${sessionScope.pONumb} />
or:
或者:
<div id="product" data-prodnumber="${sessionScope.pONumb}" />
Then in JS:
然后在JS中:
// you can find a more efficient way to select the input you want
var inputs = document.getElementsByTagName("input"), len = inputs.length, i, pONumb;
for (i = 0; i < len; i++) {
if (inputs[i].name == "pONumb") {
pONumb = inputs[i].value;
break;
}
}
or:
或者:
var product = document.getElementById("product"), pONumb;
pONumb = product.getAttribute("data-prodnumber");
The inline example is the most straightforward, but if you then want to store your JavaScript code as an external resource (the recommended way) it won't be feasible.
内联示例是最直接的,但是如果您想将 JavaScript 代码存储为外部资源(推荐方式),这将是不可行的。
<script>
var pONumb = ${sessionScope.pONumb};
[...]
</script>
回答by user2552572
<%
String session_val = (String)session.getAttribute("sessionval");
System.out.println("session_val"+session_val);
%>
<html>
<head>
<script type="text/javascript">
var session_obj= '<%=session_val%>';
alert("session_obj"+session_obj);
</script>
</head>
</html>
回答by Nimesh Dadhaniya
Below code may help you to achieve session attribution inside java script:
下面的代码可以帮助您在java脚本中实现会话属性:
var name = '<%= session.getAttribute("username") %>';
回答by Amit Vishwakarma
<?php
$sessionDetails = $this->Session->read('Auth.User');
if (!empty($sessionDetails)) {
$loginFlag = 1;
# code...
}else{
$loginFlag = 0;
}
?>
<script type="text/javascript">
var sessionValue = '<?php echo $loginFlag; ?>';
if (sessionValue = 0) {
//model show
}
</script>