Show List

JSP declarations

JSP (JavaServer Pages) declarations allow you to declare variables, methods, and other program elements directly within your JSP page. Here's an example of how to use JSP declarations:

jsp
Copy code
<%! int num = 42; String message = "Hello, JSP!"; %> <html> <body> <p>The value of num is <%= num %></p> <p>The message is <%= message %></p> </body> </html>

In this example, we're using a JSP declaration to declare two variables, num and message. The declaration is delimited by the characters <%! and %>. The variables are then used later in the JSP page, within an HTML paragraph (<p>) tag.

JSP declarations can contain any valid Java code, including class and interface definitions, method declarations, and variable declarations. Here are some more examples of JSP declarations:

jsp
Copy code
<!-- Declare a method --> <%! public int add(int a, int b) { return a + b; } %> <!-- Declare a constant --> <%! final int MAX_NUM = 100; %> <!-- Declare a class --> <%! public class Person { private String name; private int age; // constructor and methods omitted for brevity } %>



    Leave a Comment


  • captcha text