JSP/Servlet | JSP(Java Server Pages) | Defining Methods and Fields
Now you can write and execute multi-line processing in Java. But this feels a little strange. Java is not a language where you simply write a series of statements and run them like this. In general, Java is a language where you define classes, then define methods and fields inside them, and execute them.
You may wonder whether it is possible to define and call methods when writing executable statements in this way. There is no need to worry. JSP provides a dedicated tag for defining methods properly.
That tag is <%! ~ %>. If you write method definitions or variable declarations between <%! and %>, you can use them directly in the code. You can declare not only methods but also variables, and they can be used like global variables. In other words, variables declared inside <%! ~ %> can be used from other methods and elsewhere. You can think of them as playing the same role as class fields.
Now let’s make a simple usage example by modifying the previous example further.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%!
String formatPattern = "yyyy-MM-dd";
String result = "none.";
void setFormatPattern(String f) {
formatPattern = f;
}
void printToday() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat(formatPattern);
result = format.format(calendar.getTime());
}
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample jsp</title>
<style>
h1 {font-size:16pt; background:#AAFFAA; padding:5px; }
</style>
</head>
<body>
<h1>Sample jsp page</h1>
<p>This page is a sample.</p>
<% printToday(); %>
<p><%=result %></p>
<% setFormatPattern("MMMM dd, yyyy"); %>
<% printToday(); %>
<p><%=result %></p>
</body>
</html>
Here, the formatPattern variable stores the text that represents the date format, and the result variable stores the formatted date text. It also provides two methods: setFormatPattern, which sets the date format, and printToday, which assigns today’s date to the result variable in formatted form.
This example outputs text twice. If you look at that part, it is written as follows.
<% printToday(); %>
<p><%=result %></p>
<% setFormatPattern("MMMM dd, yyyy"); %>
<% printToday(); %>
<p><%=result %></p>
The flow is to set the format with setFormatPattern, assign the formatted text with printToday, and output it with <%=result %>. You can see that each method and variable is working properly.
This code also shows another important point: JSP Java code is treated as one integrated whole even if it is written across multiple tags. The processing to execute does not need to be collected into a single <% ~ %> tag. Even if it is divided across several tags, everything is processed continuously as one whole. This is a very important characteristic of JSP, so be sure to remember it.