Is it possible to put JSP code in a javascript method?


Ad

Advertisements

D

David Dorward

Ken said:
Can and how does one put JSP code inside javascript code.

Yes. The same way as one puts JSP code inside HTML code (although you also
have to write JSP to output a content type header of text/css, since it
would (I believe) otherwise default to text/html).
 
L

Lachlan Hunt

Ken said:
Can and how does one put JSP code inside javascript code. Thanks

JSP can be written within a section of JavaScript, so long as it is
processed by the server before it is output, and the browser only
recieves the resultant JavaScript. If you were wanting a browser to
understand the JSP, then no, you can't.

eg. If this is your script.

<script type="text/javascript">
<%
/*
* Code to read from DB or other source, or whatever you need.
* Say, for example, the string "hello world" is returned, and
* set to the message variable
*/
message = "hello world";
%>
var msg = "<%=message%>";
alert(msg);
</script>

From the above JSP, the browser would recieve and execute the following
script:

<script type="text/javascript">
var msg = "hello world";
alert(msg);
</script>
 
T

Toby Inkster

David said:
Yes. The same way as one puts JSP code inside HTML code (although you
also have to write JSP to output a content type header of text/css,

Odd choice of Content-Type header.
 
Ad

Advertisements


Top