• Post Reply Bookmark Topic Watch Topic
  • New Topic

jsp in javascript?  RSS feed

 
Ganesh N
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to have jsp code within javascript?
Is it possible to create functions in jsp?
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Richard Barnes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!