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

How can i convert java String to javascript string? - Stack Overflow

programmeradmin1浏览0评论

i used these code but nothing seen on document

            <%String movie_name ="Matrix"; %>
            <script type="text/javascript">
                var movie_name="";
                movie_name= <%= movie_name%>;
                document.write(movie_name);
            </script>

so anyone can help me to convert java string to javascript string ?

i used these code but nothing seen on document

            <%String movie_name ="Matrix"; %>
            <script type="text/javascript">
                var movie_name="";
                movie_name= <%= movie_name%>;
                document.write(movie_name);
            </script>

so anyone can help me to convert java string to javascript string ?

Share asked Feb 27, 2012 at 8:05 gezgingezgin 2074 silver badges11 bronze badges 2
  • 1 Is it a JSP page? Are you setting request.setAttribute("movie_name", "The Matrix") or a parameter names movie_name? Did you read this tutorial. Did not downvote though. – Nishant Commented Feb 27, 2012 at 8:08
  • oh sorry, I really did not meant to embarrass you. The question looked like it could have been solved by a little more reading of JSP specs or a tutorial. – Nishant Commented Feb 27, 2012 at 8:29
Add a ment  | 

2 Answers 2

Reset to default 6

This might do it (missing quotes):

movie_name="<%= movie_name%>"

Also looking at your sample code you can replace it pletely with:

<%= movie_name%>

Finally consider using jstl.

you need to wrap your output in quotes (I'm assuming this is JSP?)

movie_name = "<%= movie_name %>";

See, when this is written out the browser tries to interpret it, so without quotes you wind up with something that looks like...

movie_name = Men In Black;

Since this is obviously a massive syntax fail, the browser just quits trying and silently fails (though you should see a log of what it didn't like).

When you wrap the output in quotes then everything falls into place HOWEVER, make sure to convert any " in your java string to \" when you print it out, or you'll have more of the same trouble.

However, as the other answers suggest, you're re-inventing the wheel here and should just do this the prescribed way, as per Nishant's advice.

发布评论

评论列表(0)

  1. 暂无评论