Forums Register Login

jsp in javascript?

+Pie Number of slices to send: Send
Is it possible to have jsp code within javascript?
Is it possible to create functions in jsp?
+Pie Number of slices to send: Send
"Ganesh N",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
+Pie Number of slices to send: Send
Hi Ganesh,
JSP code can be mixed with Javascript code but it is important to consider when each type is run:
Javascript is run as part of the web page dynamically while the user is displaying the page.
JSP is compiled and run before the page is displayed to the user and determines the content of the page.
So you could have the following Javascript code with JSP in it which displays an alert message to the user with the compiled message string 'I was compiled earlier' produced during JSP compilation:
// extract from a jsp page
<% String messageText = "I was compiled earlier"; %> //jsp
<script> //javascript
alert("The message is <%= messageText %>"); //
</script> //
While this script could be triggered at any time during the page display, the 'messageText' string will only change if the page is redisplayed (ie recompiled).
You could also use JSP functions to decide what Javascript will be published in the page, eg:
<% boolean boolSayHello = true; %>
<% if (boolSayHello == true) { %> //jsp
<script> alert("Hello"); </script> //javascript
<% } else { %> //jsp
<script> alert("Goodbye!"); </script> //javascript
<% } %> //jsp
In this example the boolean is set to true so the alert message 'Hello' would be displayed. The script would run when the page is displayed but the jsp would be run when during page compilation before you see it.
Hope this answers your question.
Regards,
Richard
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 667 times.
Similar Threads
JSP and javascript
Can Javascript variable store into JSP variable
javascript and jsp
How can an applet access url parameters?
How to pass JavaScript variable into JSP variable
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:49:44.