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");

Share Improve this question edited Sep 30, 2015 at 13:12 Scorpio 2,3272 gold badges27 silver badges46 bronze badges asked Sep 30, 2015 at 12:41 TomTom 2,4825 gold badges29 silver badges47 bronze badges 5
  • 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
Add a ment  | 

3 Answers 3

Reset to default 3

You 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