Declare tag methods in jsp

JSP is a extended technology to the java servlet that allows to keep separate the dynamic part of your pages from the static HTML.

Declare tag methods in jsp

Declare tag methods in jsp

     

JSP is a extended technology to the java servlet that allows to keep separate the dynamic part of your pages from the static HTML. In the code of web page HTML will be as it is but dynamic part is enclosed in special tag , most of which start with "<%" and end with "%>". This is known as scriptlet part of the page.

We can declare the variables and methods in scriptlet part but the scope of these members will be only in same block in which they are declared, to resolve this problem Jsp provide special type of declaration which have scope throughout the page. This special type of declaration uses tags given below...

Declare variables : <%! int number; %>

Declare methods : <%!  void display() { out.println("My JSP Page");  }  %>

Before run this jsp code create a new directory named "user" in the tomcat-6.0.16/webapps and paste WEB-INF directory in same.

declaration_tag_method.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%!
  private String getName() {
    return "Mahendra Singh";
  }
  private String phone() {
    return "9990254913";
  }
  private String getCity() {
      return "Delhi";
  }
%>
<html>
    <head>
        <TITLE>Example of Declaration Tag - Methods</TITLE>
	</HEAD>
    <BODY bgcolor="#6E6E6E">
	   <font size="+3" color="#F2F2F2">
         Information about <%= getName() %>...<br>
         Contact Number : <%= phone()%><br>
         City : <%= getCity() %>
	   </font>
    </body>
</html>

Save this code as a .jsp file named "declaration_tag_method.jsp" in the directory Tomcat-6.0.16/webapps/user/ and you can run this jsp page with following url in address bar of the browser http://localhost:8080/user/declaration_tag_method.jsp

Download Source Code