In this tutorial you will learn about JSP declaration.
JSP Declaration
In this tutorial you will learn about JSP declaration.
Declaration in JSP is used to declare methods or fields as you have declared in the servlet body. In JSP that you have declared the methods or fields inside the declaration are put inside the body of servlet class.
Syntax
<%! code %>
Here 'code' is a java code that you have declare a fields or methods.
Here I am giving a simple example which will demonstrate you to how can you declare fields or methods inside the declaration.
Example :
declaration.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>JSP Declaration Example</title> </head> <body> <%! int i=5; String str= "Hi"; public String getString() { return "Bye"; } %> <table border= "1"> <tr><td>Value of Integer</td> <td><% out.println(i);%></td> </tr> <tr> <td>Value of String</td> <td><% out.println(str); %></td> </tr> <tr> <td>Value of method getString()</td> <td><% out.println(getString()); %></td> </tr> </table> </body> </html>
Output :
When you will execute the above example you will get the output as :
[ 0 ] Comments