Reputation: 77
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.
Upvotes: 1
Views: 894
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
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
Reputation: 1938
Enclose the value in ''. This should work
<input id='error' type='hidden' value='<%=(String)session.getAttribute("error")%>'/>
Upvotes: 3