一、说明几点:
(1)过程描述:
Jsp文件首先在服务器端生成中间的Java文件,再生成Class,然后以html的形式返回到客户端。
所以说,一个jsp文件首先执行其中的java代码,然后生成html。
(2)类型
a.java类型(就不描述了)
b.javascript 的类型
string, number, Boolean,Undefined and Null,
object, Array, Date, Math 等
javascript 是一种Dynamic Typing,Every JavaScript variable has a data type,
but the type is inferred from the variable’s content.
//var favNumber;
//favNumber = 3;
//favNumber = "San Diego";
//First, when the variable favNumber is declared, it is empty. In fact,
//its data type is actually the type undefined. Then we assign it to the number 3,
//so its data type is 3. Next we reassign it to the string "San Diego",
//so the variable’s type is now string. As you can see, types are inferred from content,
//and a variable automatically takes on the type of the data it contains.
二、交互
1 java --> html
直接使用表达式 <%= variable %>
// <link rel="stylesheet" href="<%= urlPath %>/cws/css/cws.css" type="text/css">
2 java --> javascript
由于两种类型的不同,如果java的值能够转化成数字。
String i ="1.02";
Integer i = new Integer(22);
float i = 10;
都可以直接和javascript的类型交互,即直接传给javascript变量
// <input type="button" name="back" value="back" onClick ="back(<%= i %>)" >
// 也可以<input type="button" name="back" value="back" onClick ="back('<%= i %>')" >
如果java的值不能够转化成数字
String s ="1.jsp";
需要转化为javascript的字符串类型
// <input type="button" name="back" value="back" onClick ="back('<%= s %>')" >
// <% String url = "javascript:back('" + s + "')"; %>
// <input type="button" name="back" value="back" onClick ="<%=url%>" >
3. html --> java
<%java code...
%>
html code....
<%java code...
%>
4. javascript --> java
// <% String url = "javascript:back('" + s + "')"; %>
// <input type="button" name="back" value="back" onClick ="<%=url%>" >