admin管理员组文章数量:1435859
I'm trying to pass a variable between two jsp files, but when I e to use them, I get an error saying that 'session is undefined'
I'm using the following:
Jsp1:
//following function is called on a button press
function clickAndClose()
{
session.setAttribute("test", "some value here");
window.close();
}
Jsp2:
<!DOCTYPE html>
<html>
<body>
Show Selected RS Number
<script language="javascript">
String number = session.getAttribute("test");
session.removeAttribute("test");
document.write(number);
</script>
</body>
</html>
The fact I get an error on session.setAttribute confuses me, as I thought session was an implicit object (included?) ?
Note: I'm getting an error on session.setAttribute("test", "some value");
, saying that session is undefined, NOT String text = session.getAttribute("test");
I'm trying to pass a variable between two jsp files, but when I e to use them, I get an error saying that 'session is undefined'
I'm using the following:
Jsp1:
//following function is called on a button press
function clickAndClose()
{
session.setAttribute("test", "some value here");
window.close();
}
Jsp2:
<!DOCTYPE html>
<html>
<body>
Show Selected RS Number
<script language="javascript">
String number = session.getAttribute("test");
session.removeAttribute("test");
document.write(number);
</script>
</body>
</html>
The fact I get an error on session.setAttribute confuses me, as I thought session was an implicit object (included?) ?
Note: I'm getting an error on session.setAttribute("test", "some value");
, saying that session is undefined, NOT String text = session.getAttribute("test");
-
2
session
is indeed an implicit object. Better to show some code to find out what is going on... – wero Commented Sep 30, 2015 at 12:49 - Tom Can you please show the jsp – Siva Kumar Commented Sep 30, 2015 at 12:55
- if my memory doesn't betray me, the plete signature is Object getAttribute(String name), shouldn't you need a cast there? – BigMike Commented Sep 30, 2015 at 13:00
- See updated question :) – Tom Commented Sep 30, 2015 at 13:05
- I'm still missing something, the code you add seems a mix of java and javascript. Are you sure you're not mixing client side and server side worlds? AFAIK there's no direct way to access Session from javascript. – BigMike Commented Sep 30, 2015 at 13:07
3 Answers
Reset to default 3You are trying to use session
inside <script language="javascript">
tag, it is not available there. Use it inside scriptlet
or jstl
tags.
Javascript is executed in client side, whereas jsp implicit objects are available and executed in server side
You can't access directly the session in javascript, thou you can trick it with something like:
<!DOCTYPE html>
<html>
<body>
Show Selected RS Number
<script language="javascript">
var number = '<%= session.getAttribute("test") %>';
document.write(number);
</script>
</body>
</html>
<%HttpSession sess = request.getSession(true);
sess.setAttribute("test", "some value");%>
the above code will give you the session object which is required. Use of EL (expression language) and JSTL tags is highly remended. For example, here you could use EL as
<td><input type="text" value="${test}" /></td>
本文标签: javascriptJSPsession is undefinedStack Overflow
版权声明:本文标题:javascript - JSP - session is undefined? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745669480a2669485.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论