Ugluk
Ugluk

Reputation: 77

Trouble setting session attribute in jsp

I'm trying to set a string as a session attribute in a jsp as seen below.

session.setAttribute("error","You must be logged in to access this page.");

I then have a pop up that displays the message in the code below.

<input id='error' type='hidden' value=<%=(String)session.getAttribute("error")%>>
<script>
    if(document.getElementById('error').value != "null"){
        window.alert(document.getElementById('error').value);
    }
</script>

But when I test this the pop up window only displays the word You.

enter image description here

Upvotes: 1

Views: 894

Answers (3)

Damien
Damien

Reputation: 4121

Could you add a javascript variable and use that instead?

    <script>
    var errorMessage = '<%=(String)session.getAttribute("error")%>';

    if(errorMessage != null && errorMessage.length > 0){
        window.alert(errorMessage);
    }
</script>

Upvotes: 0

Foxy
Foxy

Reputation: 416

Not sure if it helps but try this:

String value = "You must be logged in to access this page."    
session.setAttribute("error", value);

It seems you already getting first string "You" so it might be better if you save whole sentence in object(String).

Sorry for putting it in answer section as I cannot comment yet(need 50 points to comment by rules).

Upvotes: 0

ahoxha
ahoxha

Reputation: 1938

Enclose the value in ''. This should work

<input id='error' type='hidden' value='<%=(String)session.getAttribute("error")%>'/>

Upvotes: 3

Related Questions