最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - JSP - session is undefined? - Stack Overflow

matteradmin9PV0评论

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>
Post a comment

comment list (0)

  1. No comments so far