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 namesmovie_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
2 Answers
Reset to default 6This 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.