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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top