What is the difference between <%! %>
and <% %>
in JSP?
<%! %>
are JSP Declaration tags while <% %>
are JSP Scriptlet tags.
Any code you put in scriptlets gets put in the JSPs _jspService()
method when it's compiled (the analogue of a Servlet's doGet
, doPost
,... methods). This is what you'd usually write your Java code in.
But what if you want to declare new methods in your JSP class or declare instance variables? That's when you'd use the declaration tags. Any thing you put in there gets put into the JSP, outside of the _jspService()
method.