3.5.2 声明

3.5.2 声明

声明以<%!开始,并以%>结束。可以在JSP页面中声明一个变量或方法,以便在其他地方调用.

实例

declarationTest.jsp页面中声明了一个名为getTodaysDate的方法,如下所示

1
2
3
4
5
6
7
8
9
10
11
<%!
public String getTodaysDate() {
return new java.util.Date();
}
%>
<html>
<head><title>Declarations</title></head>
<body>
Today is <%=getTodaysDate()%>
</body>
</html>

声明可以出现的位置

JSP页面中,一个声明可以出现在任何地方,并且一个页面可以有多个声明。

使用声明来重写JSP页面

可以使用声明来重写JSP页面,实现类的initdestroy方法。具体做法是通过声明jspInit方法,来重写init方法。通过声明jspDestroy方法,来重写destroy方法。

jspInit和jspDestroy方法说明

这两种方法说明如下:

  • jspInit。这个方法类似于 javax.servlet.Servlet init方法。**JSP页面在初始化时调用jspInit**。不同于init方法,jspInit没有参数。还可以通过隐式对象config访问ServletConfig对象。
  • jspDestroy。这个方法类似于Servletdestroy方法,在JSP页面将被销毁时调用。

实例

lifeCycle.jsp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%!
public void jspInit() {
System.out.println("jspInit ...");
}
public void jspDestroy() {
System.out.println("jspDestroy ...");
}
%>
<html>
<head><title>jspInit and jspDestroy</title></head>
<body>
Overriding jspInit and jspDestroy
</body>
</html>

lifeCycle.jsp页面会被转换成如下Servlet

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Generated by the Jasper component of Apache Tomcat Version: Apache
* Tomcat/8.5.35 Generated at: 2019-03-28 08:43:25 UTC Note: The last modified
* time of this file was set to the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class lifeCycle_jsp
extends
org.apache.jasper.runtime.HttpJspBase
implements
org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports
{
public void
jspInit()
{
System.out.println("jspInit ...");
}
public void
jspDestroy()
{
System.out.println("jspDestroy ...");
}
private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory
.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
private static final java.util.Set<java.lang.String> _jspx_imports_packages;
private static final java.util.Set<java.lang.String> _jspx_imports_classes;
static {
_jspx_imports_packages = new java.util.HashSet<>();
_jspx_imports_packages.add("javax.servlet");
_jspx_imports_packages.add("javax.servlet.http");
_jspx_imports_packages.add("javax.servlet.jsp");
_jspx_imports_classes = null;
}
private volatile javax.el.ExpressionFactory _el_expressionfactory;
private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long>
getDependants()
{
return _jspx_dependants;
}
public java.util.Set<java.lang.String>
getPackageImports()
{
return _jspx_imports_packages;
}
public java.util.Set<java.lang.String>
getClassImports()
{
return _jspx_imports_classes;
}
public javax.el.ExpressionFactory
_jsp_getExpressionFactory()
{
if (_el_expressionfactory == null) {
synchronized (this) {
if (_el_expressionfactory == null) {
_el_expressionfactory = _jspxFactory
.getJspApplicationContext(
getServletConfig().getServletContext())
.getExpressionFactory();
}
}
}
return _el_expressionfactory;
}
public org.apache.tomcat.InstanceManager
_jsp_getInstanceManager()
{
if (_jsp_instancemanager == null) {
synchronized (this) {
if (_jsp_instancemanager == null) {
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory
.getInstanceManager(getServletConfig());
}
}
}
return _jsp_instancemanager;
}
public void
_jspInit()
{}
public void
_jspDestroy()
{}
public void
_jspService(final javax.servlet.http.HttpServletRequest request,
final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException
{
final java.lang.String _jspx_method = request.getMethod();
if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method)
&& !"HEAD".equals(_jspx_method)
&& !javax.servlet.DispatcherType.ERROR
.equals(request.getDispatcherType())) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED,
"JSPs only permit GET POST or HEAD");
return;
}
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
try {
response.setContentType("text/html; charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write('\r');
out.write('\n');
out.write("\r\n");
out.write("<!DOCTYPE html>\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<title>测试jspInit和jspDestroy</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("<h2>测试jspInit和jspDestroy</h2>\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)) {
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try {
if (response.isCommitted()) {
out.flush();
} else {
out.clearBuffer();
}
} catch (java.io.IOException e) {}
if (_jspx_page_context != null)
_jspx_page_context.handlePageException(t);
else
throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}

注意生成的Servlet类中的jspInitjspDestroy方法。现在可以用如下URL访问lifeCycle.jsp:
http://localhost:8080/app03a/lifeCycle.jsp
第一次访问页面时,可以在控制台上看到“jspInit...”,以及在Servlet/JSP容器关闭时看到“jspDestory...”。