Declarations

Intertech Tutorials

Declarations

Declarations are used to declare methods and variables that can be used in a JSP. Methods and variables are defined in <%! %> or <jsp:declaration> </jsp:declaration> elements. The simple examples below declare two variables.


<%! int x = 0; %>
<%! String s = “my name”; %>

These variables become member (instance) variables of the generated servlet.


public class _example extends HttpJspBase {

int x = 0;
String s = "my name";

public void service(HttpServletRequest request, HttpServletResponse) …

The use of JSP declaration to define a variable is uncommon because instance variables in servlets are shared between multiple threads.

Methods are declared using the same notation as variables.


<%! public String getWelcome(String name){
  return “Hello,” + name;
}%>

JSP method declarations become methods of the generated servlet. The method declaration above would cause the method below to be created in the servlet.


public class _example extends HttpJspBase {

public String getWelcome(String name){
return “Hello,” + name;}

public void _jspService(HttpServletRequest request, HttpServletResponse response) …

Methods don’t have access to the implicit objects (such as out).


<html>
<body>
<%
printHi();
%>
</body>
</html>

<%!
void printHi(){
for(int i = 0; i < 10; i++){
// Illegal! You don’t have access to the out object.
out.println("Hello again<br>");
}
}
%>

Think about the resulting servlet and you see “out” is a local variable to the _jspService() method. Thus, it does not have scope in the method printHi(). Out is available to scriptlets because scriptlets become a part of the service method (or _jspService() method). Work around the lack of “out” by passing the out object to the declared method.


<html>
<body>
<%
printHi(out);
%>
</body>
</html>

<%!
void printHi(javax.servlet.jsp.JspWriter out)
throws java.io.IOException {

for(int i = 0; i < 10; i++){
out.println("Hello again<br>");
}
}
%>

However, returning a String is the preferred technique to solve this problem.


<html>
<body>
<% out.println( printHi() ); %>
</body>
</html>

<%!
private String printHi(){
StringBuffer temp = new StringBuffer();
for(int i = 0; i < 10; i++){
temp.append("Hello again<br>");
}
return temp.toString();
}
%>

Because JSP declared methods don’t have access to the implicit objects like “out,” declared methods cannot mix HTML in the source code either.


<%! String printHi(){
  String rt = "";
  for(int i = 0; i < 10; i++){
  //Illegal
%>
  hello
<%
  }
  return rt;
}
%>

The rules for declaring methods are similar to declaring variables. More than one method can be declared in a single block. Methods cannot be declared inside of a scriptlet.

Declarations are used rarely, but they do have one important use. When a JSP is constructed the jspInit() method is called. When the JSP page is removed from the container's environment, the jspDestroy() method is called. By default, the jspInit and jspDestroy methods do nothing. You can override jspInit() or jspDestroy() to initialize or release resources by providing method declarations in the JSP.


<%!
public void jspInit()
{
//Insert an initialilzation
}
%>

Copyright (c) 2008-2013. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.