3.5 脚本元素

3.5 脚本元素

一个脚本程序就是一个Java代码块,脚本程序的开始符号为<%,结束符号为%>

实例

scriptletTest.jsp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% import="java.util.Enumeration"%>
大专栏  3.5 脚本元素<html>
<head>
<title>Scriptlet实例</title>
</head>
<body>
<b>Http headers:</b>
<br />
<%-- first scriptlet --%>
<%
out.print("<table>");
for (Enumeration<String> e = request.getHeaderNames();
e.hasMoreElements();)
{
String header = e.nextElement();
out.println(
"<tr><td align='right'>" + header + ":</td><td>"
+ request.getHeader(header) + "</td></tr>");
}
out.print("</table>");
String message = "Hello World";
%>
<hr />
<%-- second scriptlet --%>
<%
out.println(message);
%>
</body>
</html>

上面JSP页面中有两个脚本程序,需要注意的是定义在一个脚本程序中的变量可以被其后续的脚本程序使用
脚本程序第一行代码可以紧接<%标记,最后一行代码也可以紧接%>标记,不过,这会降低代码的可读性。

原文链接: 3.5 脚本元素

原文地址:https://www.cnblogs.com/lijianming180/p/12389459.html