最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to convert JSP request attribute to a string and assign it to an object in JavaScript - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 3

Please 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论