What is the difference between <%! %> and <% %> in JSP?

Scicare picture Scicare · Apr 1, 2011 · Viewed 8.6k times · Source

What is the difference between <%! %> and <% %> in JSP?

Answer

no.good.at.coding picture no.good.at.coding · Apr 1, 2011

<%! %> 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.