I have a Java servlet which sets an attribute on the HttpServletRequest object:
request.setAttribute("SOME_STRING", somestring);
Now, in my page.jsp, I want to set this string to be displayed in an HTML textarea...
I've been trying to do something like this, but I just can't get it to display the string:
var somestr = <%= (String) (request.getAttribute("SOME_STRING")) %>;
document.getElementById("my_textarea").value = somestr;
The textbox now displays "[Object object]" instead of the string itself.
How can I get it to display the actual string?
I have a Java servlet which sets an attribute on the HttpServletRequest object:
request.setAttribute("SOME_STRING", somestring);
Now, in my page.jsp, I want to set this string to be displayed in an HTML textarea...
I've been trying to do something like this, but I just can't get it to display the string:
var somestr = <%= (String) (request.getAttribute("SOME_STRING")) %>;
document.getElementById("my_textarea").value = somestr;
The textbox now displays "[Object object]" instead of the string itself.
How can I get it to display the actual string?
Share Improve this question asked Dec 8, 2013 at 16:53 JoshJosh 7076 silver badges22 bronze badges 1-
What do you put at somestring at
request.setAttribute("SOME_STRING", somestring);
– Masudul Commented Dec 8, 2013 at 17:08
3 Answers
Reset to default 3Please change your line from
var somestr = <%= (String) (request.getAttribute("SOME_STRING")) %>;
to
var somestr = '<%= (String) (request.getAttribute("SOME_STRING")) %>';
Enclose var somestr
with single quotations ' '
,
var somestr = '<%= (String) (request.getAttribute("SOME_STRING")) %>';//Add ' '
document.getElementById("my_textarea").value = somestr;
<script type="text/javascript">
var attrib ="<%=request.getAttribute("hello") %>";
alert(attrib);
</script>
Enclose your scriptlet with quotes.