jsp脚本元素包含3种类型:
jsp声明语句、jsp表达式、jsp Scriptlets
1.jsp声明语句:
jsp声明语句的语法格式:
<%! 变量或方法定义 %>
例子1:
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JSP test</title>
</head>
<!-- JSP声明è¯å¥ -->
<%!
public int a;
public String printStr()
{
return "JSP method";
}
%>
<body>
<%
out.println("a="+a);
a++;
%>
<br>
<%
out.println(printStr());
%>
</body>
</html>
2.jsp声明表达式:
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JSP test</title>
</head>
<body>
<%!
String str="JSP expression"; //声明变量str
%>
<!-- JSP表达式-->
<%=str%>
</body>
</html>
3.jsp Scriptlet
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JSP test</title>
</head>
<body>
<%
String str="JSP scriptlet"; //声明字符串
out.println(str); //输出字符串的值
%>
</body>
</html>