I am new to JSP,
I have method that contains loop and want to display value
<%!
private void getDemo() {
for(int i=0; i<10; i++) {
setName("Val: " + i); %>
<script language="javascript">
var name = "<%= getName()%>";
console.log(name);
</script>
<%!}
}%>
<%!
String name;
private void setName(String name) {
this.name = name;
}
private String getName() {
return this.name;
}
%>
Call
<script type = "text/javascript">
window.onload=function(){<%getDemo();%>};
</script>
Problem
But when I see the console, It only display Val: 9
How can I achieve all value? I need Script code inside Java Code. Is it Possible?
I am new to JSP,
I have method that contains loop and want to display value
<%!
private void getDemo() {
for(int i=0; i<10; i++) {
setName("Val: " + i); %>
<script language="javascript">
var name = "<%= getName()%>";
console.log(name);
</script>
<%!}
}%>
<%!
String name;
private void setName(String name) {
this.name = name;
}
private String getName() {
return this.name;
}
%>
Call
<script type = "text/javascript">
window.onload=function(){<%getDemo();%>};
</script>
Problem
But when I see the console, It only display Val: 9
How can I achieve all value? I need Script code inside Java Code. Is it Possible?
- NO. You cannot call script code inside java.. Java is a server side. script code are client side – Dineshkani Commented May 28, 2013 at 5:15
- but the last value display here... – Surendra Jnawali Commented May 28, 2013 at 5:18
- In browser you don't see any JSP or java code. What you see is just a plain HTML with Javascript and/or CSS. All these things can just be the output of the JSP/servlet code (not JSP or java itself) which runs on the server. – me_digvijay Commented May 28, 2013 at 5:22
- 1 @SJnawali when your code runs all the jsp code are run first and then it executes the script code. Read the JSP life cycle tutorialspoint./jsp/jsp_life_cycle.htm and also read about jsp tags dont use jsp declaration tag in all your code – Dineshkani Commented May 28, 2013 at 5:26
- @Dineshkani: yes you are correct...hmm thanks for information. – Surendra Jnawali Commented May 28, 2013 at 5:28
1 Answer
Reset to default 1Your Javascript runs client side in the browser so will not normally interact with your Java code running server side