JSP Declaration Tag

JSP Declaration Tag

he JSP declaration tag is used to declare fields and methods.The code written inside the jsp declaration tag is placed outside the service() method of auto generated servlet.So it doesn’t get memory at each request.

Syntax of Declaration Tag


<%! declaration %>


Example of Declaration Tag

<html>
    <head>
        <title>My First JSP Page</title>
    </head>
   <%!
       int count = 0;
   %>
  <body>
        Page Count is:  
        <% out.println(++count); %>   
  </body>
</html>

The above JSP page becomes this Servlet


 

public class hello_jsp extends HttpServlet
{
  int count=0;
  public void _jspService(HttpServletRequest request, HttpServletResponse response) 
                               throws IOException,ServletException
    {
      PrintWriter out = response.getWriter();
      response.setContenType("text/html");
      out.write("<html><body>");
      
      out.write("Page count is:");
      out.print(++count);
      out.write("</body></html>");
   }
}

Leave a comment